home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_07 / storn2 / cck.c < prev    next >
C/C++ Source or Header  |  1993-11-11  |  162KB  |  3,087 lines

  1. /**C*M****************************************************************
  2. **                                                                  **
  3. ** MODULE_NAME : cck.c                                              **
  4. ** VERSION     : 1.1                                                **
  5. ** DATE        : October 93                                         **
  6. ** LONG_NAME   : C Conventions checker                              **
  7. **                                                                  **
  8. **                                                                  **
  9. ** SP_NO       : C-coding conventions                               **
  10. ** SUBSYSTEM   : -                                                  **
  11. ** SYSTEM_TYPE : PCD-3T                                             **
  12. **                                                                  **
  13. ** AUTHOR      : Helene Ballay                                      **
  14. ** SUBSTITUTE  : Dr. Rainer Storn                                   **
  15. **                                                                  **
  16. ** DESCRIPTION : This program has been written to provide a tool    **
  17. **               for checking C coding conventions (please see      **
  18. **               the corresponding contribution in the C Users      **
  19. **               Journal in 1994). The program has been developed   **
  20. **               by using the Microsoft C compiler version 6.0.     **
  21. **                                                                  **
  22. ** REMARKS     : cck may be used for any commercial and non-        **
  23. **               commercial purpose. If you want to modify the      **
  24. **               program to fit your requirements feel free to do   **
  25. **               so but you should make a reference to our          **
  26. **               original source code or to the above paper.        **
  27. **               If you like the program we would be glad to        **
  28. **               receive a symbolic appreciation of $1. A few lines **
  29. **               containing your opinion on our program or sugge-   **
  30. **               stions for improvements will do as well.           **
  31. **               Please send your contribution to the following     **
  32. **               address:                                           **
  33. **                           Rainer Storn                           **
  34. **                           Schlehenring 20a                       **
  35. **                           D-85551 Kirchheim b. Muenchen          **
  36. **                           Germany                                **
  37. **                                                                  **
  38. ** FUNCTIONS   : see function prototypes section                    **
  39. ** MODULES     : none, this is a single module program.             **
  40. **                                                                  **
  41. ***C*M*E*************************************************************/
  42.  
  43. /**H*O*C**************************************************************
  44. **                                                                  **
  45. ** No.!Version! Date ! Request !    Modification           ! Author **
  46. ** ---+-------+------+---------+---------------------------+------- **
  47. **  1 + 001   +9/9/93+   -     + change c=getchar() into   +  Storn **
  48. **    +       +      +         + c=(CHAR)getchar() in main().       **
  49. **  2 + 001  +9/15/93+   -     + adjusted *false_ptr[] in  +  Storn **
  50. **    +      +       +         + ptr_name().               +        **
  51. **  3 + 001  +9/15/93+   -     + adjusted *pat_bis[] and   +  Storn **
  52. **    +      +       +         + *false_ptr[] in right_place().     **
  53. **  4 + 001  +9/15/93+   -     + included a "busy"-indicator+ Storn **
  54. **  5 + 001  +9/16/93+   -     + changed or added several  +  Storn **
  55. **    +      +       +         + comments.                 +        **
  56. **  6 + 001  +9/16/93+   -     + checked return value of   +  Storn **
  57. **    +      +       +         + malloc() in find_keyword().+       **
  58. **  1 + 1.0  +9/17/93+   -     + changed { into } in some  +  Storn **
  59. **    +      +       +         + places of *pattern_ptr[]  +        **
  60. **    +      +       +         + in right_place().         +        **
  61. **  2 + 1.0  +9/22/93+   -     + separator @(BAR) included.+  Storn **
  62. **    +      +       +         + This affected compile(),  +        **
  63. **    +      +       +   -     + part_match()              +        **
  64. **  3 + 1.0  +9/24/93+   -     + Did some speedup by intro-+  Storn **
  65. **    +      +       +         + ducing bar_flg in compile()+       **
  66. **    +      +       +         + and part_match() and mode +        **
  67. **    +      +       +         + in part_match().          +        **
  68. **  4 + 1.0  +10/2/93+   -     + removed global pointer    +  Storn **
  69. **    +      +       +         + save2_ptr and used local  +        **
  70. **    +      +       +         + pointers save2_pptr and   +        **
  71. **    +      +       +         + save3_pptr instead.       +        **
  72. **  5 + 1.0  +10/2/93+   -     + corrected some patterns in+  Storn **
  73. **    +      +       +         + right_place(). It is not  +        **
  74. **    +      +       +         + good to place a star im-  +        **
  75. **    +      +       +         + mediately after a class.  +        **
  76. **  6 + 1.0  +10/2/93+   -     + placed reinitialization   +  Storn **
  77. **    +      +       +         + section into BAR-part of  +        **
  78. **    +      +       +         + part_match().             +        **
  79. **  7 + 1.0  +10/3/93+   -     + added the requirement that+  Storn **
  80. **    +      +       +         + pointer to pointer should +        **
  81. **    +      +       +         + either be replaced by in- +        **
  82. **    +      +       +         + dex notation or have the  +        **
  83. **    +      +       +         + suffix _pptr.             +        **
  84. **                                                                  **
  85. ***H*O*C*E***********************************************************/
  86.  
  87. /*----------------------------------------------------------------------
  88. -----------------------------Includes-----------------------------------
  89. ----------------------------------------------------------------------*/
  90.  
  91. #include "stdlib.h"
  92. #include "stdtyp.h"
  93. #include "stdio.h"
  94. #include "string.h"
  95. #include "ctype.h"
  96.  
  97. /*----------------------------------------------------------------------
  98. ------------------------External Functions------------------------------
  99. ----------------------------------------------------------------------*/
  100.  
  101.  
  102. /*----------------------------------------------------------------------
  103. ---------------Global declarations--------------------------------------
  104. ----------------------------------------------------------------------*/
  105.  
  106. #define FILEN    250000                    /* No.of CHARs in file     */
  107. #define LINLEN   150                       /* assumed maximum line    */
  108.                                            /* length.                 */
  109. #define PMAX     256                       /* max size of pattern     */
  110.                                            /* for one keyword         */
  111. #define M_NCLMAX 3000                      /* max of non comment lines*/
  112.                                            /* in a module             */
  113. #define F_NCLMAX 200                       /* max of non comment lines*/
  114.                                            /* in a function           */
  115. #define BR_ERROR 0                         
  116.        /* all those defined values must fit in one character */
  117.        /* variable. They will be used to easen the task of   */
  118.        /* pattern searching.                                 */
  119. #define ACHAR   1                       /* a character                */
  120. #define BOL     2                       /* beginning of a line        */
  121. #define EOL     3                       /* end of line                */
  122. #define ANY     4                       /* any char but new-line      */
  123. #define CLASS   5                       /* in the following class     */
  124. #define NCLASS  6                       /* not in the following class */
  125. #define STAR    7                       /* zero or more occurences    */
  126. #define PLUS    8                       /* one or more occurences     */
  127. #define MINUS   9                       /* zero or one occurence      */
  128. #define NALPHA  10                      /* any alphanumeric           */
  129. #define VAR     11                      /* any char of a var name     */
  130. #define RANGE   12                      /* in range of the 2 next char*/ 
  131. #define ENDPAT  13                      /* end of pattern             */
  132. #define BAR     14                      /* logical or                 */
  133.  
  134. /*----------------------------------------------------------------------
  135. -----------------Typedefs-----------------------------------------------
  136. ----------------------------------------------------------------------*/
  137.  
  138. typedef struct
  139. /*****************************
  140. **                          **
  141. ** Location of keywords in  **
  142. ** source_file[].           **
  143. **                          **
  144. *****************************/
  145. {
  146.  CHAR *end_ptr;                        /* end of keyword              */
  147.  LONG line_no;                         /* corresponding line number.  */
  148. }T_LOCA;
  149.  
  150.  
  151. /*----------------------------------------------------------------------
  152. -----------------Global variables---------------------------------------
  153. ----------------------------------------------------------------------*/
  154.  
  155. CHAR    source_file[FILEN];   /* Holds source file. source_file[]     */
  156.                               /* must be global in order to allow     */
  157.                               /* a huge value for FILEN (at least for */
  158.                               /* Microsoft C 6.0 for DOS).            */
  159.  
  160.  
  161. /*----------------------------------------------------------------------
  162. -----------------------function prototypes------------------------------
  163. ----------------------------------------------------------------------*/
  164.  
  165. void   main(SHORT argc, CHAR *argv[]);
  166. LONG   read_file(CHAR source_file[], CHAR *argv[]);
  167. LONG   braces_etc(CHAR source_file[], LONG nb_line, FILE *fout_ptr, CHAR *argv[]);
  168.  
  169. void   compile(CHAR *source_ptr, CHAR pbuf[]);
  170. CHAR   *comp_class(CHAR *source_ptr, CHAR *cl_st_ptr, CHAR pbuf[], CHAR *p_ptr, CHAR **save3_pptr);
  171. void   store(CHAR op, CHAR pbuf[], CHAR *p_ptr);
  172.  
  173. T_LOCA find_keyword(CHAR *start_ptr, LONG line_st, LONG line_end, CHAR pbuf[]);
  174. CHAR   *part_match(CHAR *start_ptr, CHAR pattern[], LONG *l_ptr, CHAR **save2_pptr, CHAR mode);
  175.  
  176. void   forbidden_keyword(CHAR source_file[], LONG nb_line, FILE *fout_ptr);
  177. void   right_place(CHAR source_file[], LONG nb_line, FILE *fout_ptr);
  178. void   user_type_name(CHAR source_file[], LONG nb_line, FILE *fout_ptr);
  179. void   ptr_name(CHAR source_file[], LONG nb_line, FILE *fout_ptr);
  180. void   switch_stat(CHAR source_file[], LONG nb_line, FILE *fout_ptr);
  181. void   default_stat(CHAR source_file[], LONG nb_line, FILE *fout_ptr);
  182. void   tst_loop(CHAR source_file[], LONG nb_line, FILE *fout_ptr);
  183. void   lib_fct_call(CHAR source_file[], LONG nb_line, FILE *fout_ptr);
  184. void   val_and_ad(CHAR source_file[], LONG nb_line, FILE *fout_ptr);
  185. void   miscellaneous(CHAR source_file[], LONG nb_line, FILE *fout_ptr);
  186.  
  187. T_LOCA recurs(T_LOCA l_switch, LONG nb_line, CHAR psw[], CHAR pdef[], FILE *fout_ptr);
  188.  
  189. /*----------------------------------------------------------------------
  190. -----------------------function definitions-----------------------------
  191. ----------------------------------------------------------------------*/
  192.  
  193. void main(SHORT argc, CHAR *argv[])
  194. /**C*F****************************************************************
  195. **                                                                  **
  196. ** SRC-MODULE     : main                                            **
  197. ** VERSION        : 001                                             **
  198. ** LONG_NAME      : main                                            **
  199. **                                                                  **
  200. ** SPECIFICATION  : C coding conventions                            **
  201. ** SUBSYSTEM      : -                                               **
  202. ** SYSTEM-TYPE    : PCD-3T                                          **
  203. **                                                                  **
  204. ** AUTHOR         : Helene Ballay                                   **
  205. ** SUBSTITUTE     : Dr. Rainer Storn                                **
  206. **                                                                  **
  207. ** DESCRIPTION    :Main reads an input file, does some parsing and  **
  208. **                 writes the results on an output file. Both file- **
  209. **                 names must be provided when calling cck.         **
  210. **                 Calling syntax is:"cck <input-file> <output-     **
  211. **                 file>". Note that the filenames must be complete **
  212. **                 i.e. if the input file is "test.c" the name must **
  213. **                 be "test.c" and not just "test" as it is common  **
  214. **                 practice in application programs.                **
  215. **                                                                  **
  216. ** REMARKS        :-                                                **
  217. **                                                                  **
  218. ** FUNCTIONS      : printf(), exit(), strstr(), fopen(), getchar()  **
  219. **                  fopen(), fclose(), fprintf(), read_file(),      **
  220. **                  braces_etc(), forbidden_keyword(), right_place()**
  221. **                  user_type_name(), ptr_name(), switch_stat(),    **
  222. **                  tst_loop(), lib_fct_call(), unsgn_shift(),      **
  223. **                  val_and_ad(), micellaneous()                    **
  224. **                                                                  **
  225. ** GLOBALS        : see globals declaration section for explanation **
  226. **                                                                  **
  227. ** PARAMETERS     :argc    : holds the number of arguments in the   **
  228. **                           command line (3 in this case).         **
  229. **                 argv    : points to an array of strings. argv[0] **
  230. **                           for example points to the first string **
  231. **                           which must be "cck".                   **
  232. **                                                                  **
  233. ** PRECONDITIONS  : there must be a C source file <input_file>      **
  234. **                  which ideally should have passed compilation    **
  235. **                  without warnings (however this is not necessary)**
  236. **                                                                  **
  237. ** POSTCONDITIONS : an output file will be generated that produces  **
  238. **                  a list of warnings concerning the Coding        **
  239. **                  Conventions. For the Coding Conventions see     **
  240. **                  "Coding Conventions for C-SW Projects",         **
  241. **                  Dr. Rainer Storn, ZFE ST ACK 21.                **
  242. **                                                                  **
  243. **                                                                  **
  244. ***C*F*E*************************************************************/
  245. {
  246.  FILE   *fout_ptr;                     /* Pointer to output file.    */
  247.  LONG   nb_line;                       /* Line counter               */
  248.  LONG   nc_line;                       /* Number of noncomment lines */
  249.  CHAR   *c_ptr;
  250.  CHAR   c;
  251.  
  252. /*------Check arguments-------------------------------------------------*/
  253.  
  254.  if (argc NEQ 3)                                 /* number of arguments */
  255.  {
  256.     printf("\nUsage : cck <input-file> <output-file>\n");
  257.     exit(1);
  258.  }
  259.  c_ptr = strstr(argv[2],".c");       /* look for .c in output file name */
  260.  
  261.  if (( c_ptr NEQ NULL ) AND ( *(c_ptr+2) EQ '\0'))
  262.  {
  263.     printf("\nUsage : cck <input-file> <output-file>\n");
  264.     printf("The output file is not a C file, hence its name shall not end with .c \n");
  265.     exit(1);
  266.  }
  267.  fout_ptr = fopen(argv[2],"r");             /* NULL if output file does */
  268.                                                        /* already exist */
  269.  if ( fout_ptr NEQ NULL )
  270.  {
  271.     printf("\nOutput file %s does already exist, type y if you ",argv[2]);
  272.     printf("want to overwrite it, anything else if you want to exit.\n");
  273.     c = (CHAR)getchar();
  274.     if ( c NEQ 'y')
  275.       exit(1);
  276.  }
  277.  (void)fclose(fout_ptr);
  278.  
  279. /*----- Initialize variables --------------------------------------------*/
  280.  
  281.  nb_line   = 0;                                  /* set counters to zero */
  282.  nc_line   = 0;
  283.  
  284. /*-----Establish output file---------------------------------------------*/
  285.  
  286.  fout_ptr = fopen(argv[2],"w");          /* Open Output file for writing */
  287.  
  288.  if (fout_ptr EQ NULL)
  289.  {
  290.     printf("\nCannot open output file\n");
  291.     exit(1);                                 /* output file is necessary */
  292.  }
  293.  
  294.  (void)fclose(fout_ptr);                         /* Close output file.   */
  295.  
  296. /*----- Read input file -------------------------------------------------*/
  297. /*----- (replace '\n' by '\0' and return nb of lines of source_file) ----*/ 
  298.  
  299.  printf("Please wait until the figure 11 has appeared.\n");
  300.  nb_line = read_file(source_file, argv);
  301.  
  302. /*-----Check braces, brackets, parens and comments-----------------------*/
  303.  
  304.  nc_line = braces_etc(source_file, nb_line, fout_ptr, argv);
  305.  printf("1 ");
  306.  
  307.  if ( nc_line NEQ BR_ERROR )
  308.  { 
  309.     if ( nc_line > M_NCLMAX )
  310.     {
  311.        fprintf(fout_ptr, "\nWARNING : the size of this module is %ld noncommented lines ",nc_line);
  312.        fprintf(fout_ptr, "\n          more than the maximum size %ld ! ", M_NCLMAX );        
  313.     }
  314.  
  315.     /*---- Check presence of forbidden keywords -------------------------*/
  316.  
  317.     (void)forbidden_keyword(source_file, nb_line, fout_ptr);
  318.     printf("2 ");
  319.  
  320.     /*---- Check place of attributes like typedef... --------------------*/
  321.  
  322.     (void)right_place(source_file, nb_line, fout_ptr);
  323.     printf("3 ");
  324.  
  325.     /*---- Check names of user defined types ----------------------------*/
  326.  
  327.     (void)user_type_name(source_file, nb_line, fout_ptr); 
  328.     printf("4 ");
  329.  
  330.     /*---- Check that pointers names end with _ptr ----------------------*/
  331.  
  332.     (void)ptr_name(source_file, nb_line, fout_ptr); 
  333.     printf("5 ");
  334.  
  335.     /*---- Check switch statements --------------------------------------*/
  336.  
  337.     (void)switch_stat(source_file, nb_line, fout_ptr);
  338.     printf("6 ");
  339.  
  340.     (void)default_stat(source_file, nb_line, fout_ptr);
  341.     printf("7 ");
  342.  
  343.     /*---- Tests in while and if loops ----------------------------------*/
  344.  
  345.     (void)tst_loop(source_file, nb_line, fout_ptr); 
  346.     printf("8 ");
  347.  
  348.     /*---- Return values checked before use -----------------------------*/
  349.  
  350.     (void)lib_fct_call(source_file, nb_line, fout_ptr);
  351.     printf("9 ");
  352.  
  353.     /*---- Value and address of an object in the same line --------------*/
  354.  
  355.     (void)val_and_ad(source_file, nb_line, fout_ptr); 
  356.     printf("10 ");
  357.  
  358.     /*---- Miscellaneous ------------------------------------------------*/
  359.  
  360.     (void)miscellaneous(source_file, nb_line, fout_ptr);
  361.     printf("11\n");
  362.  
  363.     (void)fclose(fout_ptr); 
  364.  }
  365. }
  366.  
  367. LONG read_file(CHAR source_file[], CHAR *argv[])
  368. /**C*F****************************************************************
  369. **                                                                  **
  370. ** SRC-FUNCTION   :read_file                                        **
  371. ** VERSION        :001                                              **
  372. ** LONG_NAME      :read_file                                        **
  373. **                                                                  **
  374. ** SPECIFICATION  :C Coding Conventions                             **
  375. ** SUBSYSTEM      :-                                                **
  376. ** SYSTEM-TYPE    :PCD-3T                                           **
  377. **                                                                  **
  378. ** AUTHOR         :Dr. Rainer Storn                                 **
  379. ** SUBSTITUTE     :Helene Ballay                                    **
  380. **                                                                  **
  381. ** DESCRIPTION    :read_file opens the file specified by argv[1],   **
  382. **                 reads it, and stores it into the character array **
  383. **                 source_file[]. While doing this, all '\n' are    **
  384. **                 transformed into '\0' so that source_file[] will **
  385. **                 contain a collection of adjacent strings which   **
  386. **                 can be treated by the string processing fun-     **
  387. **                 ctions in string.h. The number of strings,       **
  388. **                 nb_line, will be returned by read_file().        **
  389. **                                                                  **
  390. ** REMARKS        :-                                                **
  391. **                                                                  **
  392. ** FUNCTIONS      :printf(), exit(), fopen(), fclose(), getc(),     **
  393. **                 feof()                                           **
  394. **                                                                  **
  395. ** GLOBALS        :source_file[]                                    **
  396. **                                                                  **
  397. ** PARAMETERS     :argv : here only argv[1] is used which points to **
  398. **                        the input file. For more infomation see   **
  399. **                        declaration in main().                    **
  400. **                                                                  **
  401. ** PRECONDITIONS  :-                                                **
  402. **                                                                  **
  403. ** POSTCONDITIONS :if input file is available, its content will be  **
  404. **                 in source_file[] with all '\n' replaced by '\0'. **
  405. **                 If the input file was too long, source_file[]    **
  406. **                 will contain a troncated version and a warning   **
  407. **                 message will be printed on the standard output.  **
  408. **                                                                  **
  409. **                                                                  **
  410. ***C*F*E*************************************************************/
  411. {
  412.  FILE *fin_ptr;                       /* File pointer to input file.*/
  413.  CHAR ch_buffer;                      /* intermediate buffer        */
  414.  CHAR eof_flag;                       /* turns 1 on EOF, 0 otherwise*/
  415.  LONG i;                              /* Universal counter          */
  416.  LONG nb_line;                        /* Number of lines            */
  417.  
  418. /*------ Open input file -----------------------------------------------*/
  419.  
  420.  fin_ptr = fopen(argv[1],"r");           /* Open input file for reading */
  421.  
  422.  if (fin_ptr EQ NULL)
  423.  {
  424.     printf("\nCannot open input file in read_file()\n");
  425.     exit(1);
  426.  }
  427.  
  428. /*----- Read source file and change all returns (\n) into ---------------*/
  429. /*----- string delimiters (\0) ------------------------------------------*/
  430.  
  431.  for (i=0; i<FILEN; i++)
  432.  {
  433.     source_file[i] = ' ';           /* initialize array s[] with blanks  */
  434.  }
  435.  
  436.  source_file[FILEN-1] = '\0';       /* indicate end of array with '\0'   */
  437.  
  438.  ch_buffer  = (CHAR)getc(fin_ptr);  /* Read first character              */
  439.  i          = 0;                    /* initialize file length counter    */
  440.  nb_line    = 0;                    /* initialize line counter           */
  441.  eof_flag   = 0;                    /* Reset eof_flag to "no EOF"        */
  442.  
  443. /*------Keep on reading as long as there is something to read and--------*/
  444. /*------there is still space left in source_file[].----------------------*/
  445.  
  446.  while ((source_file[i] NEQ '\0') AND ( eof_flag NEQ 1 ))
  447.  {
  448.     if (ch_buffer EQ '\n')
  449.     {
  450.        ch_buffer = '\0';        /* Convert newline into string delimiter */
  451.        nb_line++;               /* increment line counter                */
  452.     }
  453.     source_file[i] = ch_buffer;
  454.     i++;
  455.     ch_buffer      = (CHAR)getc(fin_ptr);         /* read next character */
  456.  
  457.     if ( feof(fin_ptr) NEQ 0)
  458.     {
  459.        eof_flag = 1;                      /* set eof_flag to "EOF found" */
  460.        source_file[i] = '\0';                    /* Indicate EOF with \0 */
  461.     }
  462.  }
  463.  
  464.  
  465.  if (eof_flag EQ 0)
  466.  {
  467.     printf("\nInput file is too large for input buffer source_file[]\n");
  468.     (void)fclose(fin_ptr);
  469.     exit(1);
  470.  }
  471.  (void)fclose(fin_ptr);                             /* close input file  */
  472.  return(nb_line);
  473. }
  474.  
  475. LONG braces_etc(CHAR source_file[], LONG nb_line, FILE *fout_ptr, CHAR *argv[])
  476. /**C*F*******************************************************************
  477. **                                                                     **
  478. ** SRC-MODULE     : braces_etc                                         **
  479. ** VERSION        : 001                                                **
  480. ** LONG_NAME      : braces_etc                                         **
  481. **                                                                     **
  482. ** SPECIFICATION  : this program is based on CC.C by T. Jennings and   **
  483. **                  David N. Smith, public domain program CUG152.15    ** 
  484. ** SUBSYSTEM      : -                                                  **
  485. ** SYSTEM_TYPE    : PCD-3T                                             **
  486. **                                                                     **
  487. ** AUTHOR         : Helene Ballay                                      **
  488. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  489. **                                                                     **
  490. ** DESCRIPTION    : braces_etc() checks the brackets, comments,        **
  491. **                  parens and doublequotes by counting them in        **
  492. **                  source_file[] and printing the result in the file  **
  493. **                  pointed to by fout_ptr. It skips over all char     **
  494. **                  within comments or doublequoted text and does not  **
  495. **                  count quoted characters. The number of lines with  **
  496. **                  "not just comments" inside, ncline, will be re-    **
  497. **                  turned.                                            **
  498. **                                                                     **
  499. ** REMARKS        :                                                    **
  500. **                                                                     **
  501. ** FUNCTIONS      : fopen(), printf(), exit(), fclose(), fprintf(),    **
  502. **                  return()                                           **
  503. **                                                                     **
  504. ** GLOBALS        : source_file[]                                      **
  505. **                                                                     **
  506. ** PARAMETERS     : nb_line : number of lines of source_file[]         **
  507. **                  fout_ptr: points to the output file                **
  508. **                  argv[]  : argv[2] points to the third  string of   **
  509. **                            the line command ie the output file name **
  510. **                                                                     **
  511. ** PRECONDITIONS  : source_file[] must contain an ASCII file with all  **
  512. **                  '\n' replaced by '\0'.                             **
  513. **                                                                     **
  514. ** POSTCONDITIONS : file pointed to by fout_ptr will contain results   **
  515. **                  of checking process.                               **
  516. **                                                                     **
  517. **                                                                     **
  518. ***C*F*E****************************************************************/
  519. {
  520.   register CHAR *c_ptr;  /* current character                          */
  521.   CHAR lastc;            /* last character                             */
  522.   CHAR nextc;            /* next character                             */
  523.   LONG parens;           /* counts opened parens                       */
  524.   LONG brackets;         /* counts opened brackets                     */
  525.   LONG comment;          /* counts opened comments                     */
  526.   LONG dblequote;        /* counts opened dblequotes                   */
  527.   LONG line;             /* current line of entire file                */
  528.   LONG ncline;           /* counts non empty lines with "not just      */
  529.                          /* comments" (entire file)                    */
  530.   LONG fline;            /* counts non empty lines of current function */
  531.   LONG fncline;          /* counts non empty lines with "not just      */
  532.                          /* comments" (current function)               */
  533.   LONG fcline;           /* counts comment lines of current function   */
  534.                          /* with or without noncomment C code.         */
  535.   CHAR *str_ptr;         /* string (line) pointer                      */
  536.   LONG sth;              /* counts anything else than spaces or  com-  */
  537.                          /* ments or quoted text (used to recognize    */
  538.                          /* ncline )                                   */
  539.   LONG message;          /* flag indicating that a warning happened    */
  540.  
  541. /*------Open output file for appending information-------------------------------*/
  542.  
  543.   fout_ptr=fopen(argv[2],"a");
  544.  
  545.   if (fout_ptr EQ NULL)
  546.   {
  547.      printf("\nCannot open output file in braces_etc()\n");
  548.      exit(1);                                           /* output file is needed */
  549.   }
  550.  
  551. /*-----Initialize variables------------------------------------------------------*/
  552.  
  553.   parens    = 0;                          /* set initial value 0 to all counters */
  554.   brackets  = 0;                          /* set initial value 0 to all counters */
  555.   comment   = 0;                          /* set initial value 0 to all counters */
  556.   dblequote = 0;                          /* set initial value 0 to all counters */
  557.   line      = 0;                          /* set initial value 0 to all counters */
  558.   ncline    = 0;                          /* set initial value 0 to all counters */
  559.   fline     = 0;                          /* set initial value 0 to all counters */
  560.   fncline   = 0;                          /* set initial value 0 to all counters */
  561.   fcline    = 0;                          /* set initial value 0 to all counters */
  562.   sth       = 0;                          /* set initial value 0 to all counters */
  563.   message   = 0;
  564.  
  565.   c_ptr     = source_file;                            /* read first character    */
  566.   lastc     = 'a';                                    /* dummy value             */
  567.   nextc     = 'a';                                    /* dummy value             */
  568.   str_ptr   = source_file;                            /* points to first char    */ 
  569.  
  570. /*----Examine source_file -------------------------------------------------------*/
  571.  
  572.  
  573.   while ( nb_line > line)                          /* still are unexamined lines */
  574.   {
  575.      /*------- skip over comments -----------------------------------------------*/
  576.  
  577.      if ((lastc EQ '/') AND ( *c_ptr EQ '*'))         /* if there is a comment   */
  578.      {
  579.         comment++;
  580.         fcline++;
  581.         do                                            /* go to end of comment    */
  582.         { 
  583.            lastc = *c_ptr;
  584.            c_ptr++;
  585.  
  586.            if ( *c_ptr EQ '\0')                       /* printing ended lines    */
  587.            {  
  588.               line++;
  589.               fcline++;
  590.               fline++;
  591.               fprintf(fout_ptr,"\nline %4ld: {%2ld} (%2ld) /*%2ld*/ \"%2ld\" ",line,brackets,parens,comment,dblequote);
  592.               fprintf(fout_ptr,"%s ", str_ptr);
  593.               str_ptr = c_ptr + 1;                                  /* next line */
  594.            }
  595.  
  596.         }while (((lastc NEQ '*') OR ( *c_ptr NEQ '/')) AND( nb_line >= line));
  597.  
  598.         if ( line > nb_line)                          /* if end of file reached  */
  599.             continue;                                 /* go back to while loop   */
  600.  
  601.         comment--;                                  /* if end of comment reached */
  602.         lastc = *c_ptr;
  603.         c_ptr++;
  604.         nextc = *(c_ptr + 1);
  605.         continue;                                  /* and  let's start again...  */
  606.      }                                             /* (go back to while loop)    */
  607.  
  608.      /*------- skip over text between double quotes -----------------------------*/
  609.      /*------- (same principle as above)-----------------------------------------*/
  610.  
  611.      else if (( *c_ptr EQ '"') AND ((lastc NEQ '\'') OR (nextc NEQ '\'')) )
  612.      {
  613.         dblequote++;
  614.         do                                         /* go to end of quoted text   */
  615.         {
  616.            lastc = *c_ptr;
  617.            c_ptr++;
  618.               
  619.            if (*c_ptr EQ '\0')                     /* printing finished lines    */
  620.            {
  621.               line++;
  622.               ncline++;
  623.               fline++;
  624.               fncline++;
  625.               fprintf(fout_ptr,"\nline %4ld: {%2ld} (%2ld) /*%2ld*/ \"%2ld\" ",line,brackets,parens,comment,dblequote);
  626.               fprintf(fout_ptr," %s ", str_ptr);
  627.               str_ptr = c_ptr + 1;                                  /* next line */
  628.            }
  629.         }while ( (( *c_ptr NEQ '"' ) OR (lastc EQ '\\')) AND ( nb_line > line) );
  630.  
  631.         if ( line > nb_line )                       /* if end of file reached    */
  632.               continue;                             /* go back to while loop     */
  633.  
  634.         dblequote--;                            /* if end of quoted text reached */
  635.         lastc = *c_ptr;
  636.         c_ptr++;
  637.         nextc = *(c_ptr +1);
  638.         continue;                                   /* and let's start again...  */
  639.     }                                               /* (go back to while loop)   */
  640.   
  641.     /*------- Brackets and parens -----------------------------------------------*/
  642.  
  643.     else if (( *c_ptr EQ '{') OR ( *c_ptr EQ '}') OR ( *c_ptr EQ '(') OR ( *c_ptr EQ ')'))
  644.     {  
  645.        if (( nextc NEQ '\'') OR (lastc NEQ '\''))        /* only non quoted ones */
  646.        {
  647.           if ( *c_ptr EQ '{')                               /* If bracket opened */
  648.           { 
  649.              brackets++;
  650.              if (brackets EQ 1)                        /* if start of a function */
  651.              {
  652.                  fline   = 0;                     /* reset all function counters */
  653.                  fcline  = 0;
  654.                  fncline = 0;      
  655.              }
  656.           }
  657.           if ( *c_ptr EQ '}')                               /* If bracket closed */
  658.           {
  659.              brackets--;
  660.              if (brackets EQ 0)                          /* if end of a function */
  661.              {
  662.                  if (fncline > F_NCLMAX)                 /* if function too long */
  663.                  {
  664.                     message++;
  665.                     fprintf(fout_ptr,"\n\nWARNING (line %4ld): This function contains %ld non comment lines of code.",line,fncline);
  666.                     fprintf(fout_ptr,"You shouldn't use more than %d.\n",F_NCLMAX);
  667.                  }
  668.                  if (fcline < (LONG)(.2 * (float)(fline)) )  /* if function not enough commented */
  669.                  {
  670.                     message++;
  671.                     fprintf(fout_ptr,"\n\nWARNING (line %4ld): this function has only %ld comment lines",line,fcline);
  672.                     fprintf(fout_ptr," for %ld lines of code, please use at least %ld comment lines (20%%).\n",fline,(LONG)(.2 * (float)(fline)));
  673.                  }               
  674.              }
  675.           }
  676.           if ( *c_ptr EQ '(')                               /* if parens opened */
  677.              parens++;
  678.           if ( *c_ptr EQ ')')                               /* if parens closed */
  679.              parens--;
  680.        } 
  681.     }
  682.     /*------- Unexpected end of comment ----------------------------------------*/
  683.  
  684.     if ((lastc EQ '*') AND ( *c_ptr EQ '/'))
  685.        comment--;
  686.  
  687.     /*------ End of line: print the line with the values of the counters -------*/
  688.  
  689.     if ( *c_ptr EQ '\0')
  690.     {
  691.        line++;
  692.        if (*(c_ptr-1) NEQ '\0')                 /* counts only non empty lines */
  693.        {
  694.           fline++;
  695.           if (sth NEQ 0)           /* a noncomment line must contain something */
  696.           {                        /* else than comment and space              */
  697.             fncline++;
  698.             ncline++;
  699.           }
  700.        }
  701.        fprintf(fout_ptr," \nline %4ld: {%2ld} (%2ld) /*%2ld*/ \"%2ld\" ",line,brackets,parens,comment,dblequote);
  702.        fprintf(fout_ptr," %s ", str_ptr);
  703.        str_ptr = c_ptr + 1;                                        /* next line */
  704.        sth = 0;                                                /* reset counter */
  705.     }
  706.  
  707.     /*------ Next character, please ! ------------------------------------------*/
  708.  
  709.     lastc = *c_ptr;
  710.     if ((lastc NEQ '\0') AND (lastc NEQ ' ') AND (lastc NEQ '/') AND (lastc NEQ '"'))
  711.         sth++;
  712.     c_ptr++;
  713.     nextc = *(c_ptr +1);
  714.  
  715.  }
  716.  
  717.  /*--------- Warning messages --------------------------------------------------*/
  718.  
  719.   if ( (comment EQ 0) AND (dblequote EQ 0) AND (brackets EQ 0) AND (parens EQ 0))
  720.   {
  721.      if (message EQ 0)                      /* if braces_etc printed no warning */
  722.      {
  723.        (void)fclose(fout_ptr);                             /* close output file */
  724.        fout_ptr = fopen(argv[2],"w");       /* reopen output file, text printed */
  725.                                             /* there by braces_etc is lost      */  
  726.        if (fout_ptr EQ NULL)
  727.        {
  728.            printf("\nCannot re-open output file\n");
  729.            exit(1);                                    /* output file is needed */
  730.        }
  731.      }
  732.      else
  733.      {
  734.         fprintf(fout_ptr,"\n\nWARNING : %ld function(s) of this program is(are) not ",message);
  735.         fprintf(fout_ptr,"enough commented, see listing above for details. \n");
  736.      }
  737.      return(ncline);
  738.   }
  739.   else
  740.   {  
  741.      if (comment NEQ 0)
  742.         fprintf(fout_ptr, "\n\n UNBALANCED COMMENTS !");
  743.  
  744.      if (dblequote NEQ 0)
  745.         fprintf(fout_ptr, "\n\n UNBALANCED DOUBLE QUOTES !");
  746.  
  747.      if (brackets NEQ 0)
  748.         fprintf(fout_ptr, "\n\n UNBALANCED BRACKETS !");
  749.  
  750.      if (parens NEQ 0)
  751.         fprintf(fout_ptr, "\n\n UNBALANCED PARENS !");
  752.  
  753.      (void)fclose(fout_ptr);
  754.      return(BR_ERROR);                      /* other tests will not be executed */
  755.   }
  756. }
  757.  
  758. T_LOCA find_keyword(CHAR *start_ptr, LONG line_st, LONG line_end, CHAR pbuf[])
  759. /**C*F*******************************************************************
  760. **                                                                     **
  761. ** SRC-MODULE     : find_keyword                                       **
  762. ** VERSION        : 001                                                **
  763. ** LONG_NAME      : find_keyword                                       **
  764. **                                                                     **
  765. **                                                                     **
  766. ** SPECIFICATION  : C coding conventions                               **
  767. ** SUBSYSTEM      : -                                                  **
  768. ** SYSTEM_TYPE    : PCD-3T                                             **
  769. **                                                                     **
  770. ** AUTHOR         : Helene Ballay                                      **
  771. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  772. **                                                                     **
  773. ** DESCRIPTION    : find_keyword() scans source_file[] starting at     **
  774. **                  start_ptr (line_st) until the beginning of line_end**
  775. **                  for the pattern already compiled in pbuf[] ( see   **
  776. **                  function compile() ), by calling part_match().     **
  777. **                  it goes one char further if no match is found,     **
  778. **                  or skip over quoted text or comment. (in this case,**
  779. **                  part_match will have put the end of text to skip   **
  780. **                  in *save2_pptr ). find_keyword will return the     **
  781. **                  first location found, (NULL,0) if none.            **
  782. **                                                                     **
  783. ** REMARKS        :                                                    **
  784. **                                                                     **
  785. ** FUNCTIONS      : malloc(), part_match(), return()                   **
  786. **                                                                     **
  787. ** GLOBALS        :                                                    **
  788. **                                                                     **
  789. ** PARAMETERS     : start_ptr points to the char in source_file where  **
  790. **                            the search shall begin.                  **
  791. **                  line_st   number of the line in source_file[] to   **
  792. **                            start the search.                        **
  793. **                  line_end  number of the line in source_file[] to   **
  794. **                            end the search.                          **
  795. **                  pbuf[]    array contains the pattern to match      **
  796. **                                                                     **
  797. ** PRECONDITIONS  : pbuf[] must be compiled ( by compile() ) before    **
  798. **                                                                     **
  799. ** POSTCONDITIONS : returns the location of the first match found or   **
  800. **                  (NULL, 0) if no match. *save2_pptr is NULL at the  **
  801. **                  end.                                               **
  802. **                                                                     **
  803. **                                                                     **
  804. ***C*F*E****************************************************************/
  805. {
  806.   T_LOCA location;               /* location of keyword                */ 
  807.   register LONG  *l_ptr;         /* current line                       */
  808.                                  /* a pointer is used because the value*/
  809.                                  /* will be changed by part_match()    */
  810.   CHAR   *st_ptr;                /* beginning of the partial search    */
  811.   CHAR   *pl_ptr;                /* place found by part_match()        */
  812.   CHAR  **save2_pptr;            /* *save2_pptr is used to know the    */
  813.                                  /* end of text being skipped by       */
  814.                                  /* part_match().                      */
  815.   CHAR   *anchor_ptr;            /* serves just as a defined location  */
  816.                                  /* in memory.                         */
  817.  
  818. /*--------- Initialize variables -----------------------------------------------*/
  819.  
  820.   st_ptr      = start_ptr;                           /* beginning of the search */
  821.   pl_ptr      = NULL;                                         /* no match found */
  822.   save2_pptr  = &anchor_ptr;       /* initialize save2_pptr to a defined value. */
  823.   *save2_pptr = NULL;                                        /* no text to skip */
  824.  
  825.   l_ptr       = (LONG *)malloc(1*sizeof(LONG));
  826.   if (l_ptr EQ NULL)
  827.   {
  828.      printf("\nmalloc() failed in function find_keyword()\n");
  829.      exit(1);
  830.   }
  831.  
  832.   *l_ptr    = line_st;                                            /* first line */
  833.  
  834.   location.end_ptr = NULL;                           /* "keyword not found (yet)*/
  835.   location.line_no = 0;                              /* "keyword not found (yet)*/
  836.  
  837. /*--------- Scan source_file starting at start_ptr for the pattern in pbuf -----*/
  838.  
  839.   if (*st_ptr EQ '\0')                 /* if search starts at the end of a line */
  840.      (*l_ptr)++;
  841.  
  842.   pl_ptr   = part_match(st_ptr, pbuf, l_ptr, save2_pptr, 1);
  843.   
  844.   while (( *l_ptr < line_end) AND ( pl_ptr EQ NULL)) 
  845.   {
  846.      if (*save2_pptr NEQ NULL )       /* if part_match() has found text to skip */
  847.      {
  848.          st_ptr = *save2_pptr;                                  /* skip over it */
  849.          *save2_pptr = NULL;                                 /* no text to skip */
  850.      }
  851.      st_ptr++;                              /* next character in source_file[] */
  852.  
  853.      if ( *st_ptr EQ '\0')                         /* if end of a line reached */
  854.        {
  855.         (*l_ptr)++;
  856.        }
  857.  
  858.      pl_ptr = part_match(st_ptr, pbuf, l_ptr, save2_pptr, 1);
  859.   }
  860.   if (pl_ptr NEQ NULL)                                     /* if keyword found */
  861.  
  862.   {
  863.      location.line_no = *l_ptr;
  864.      location.end_ptr = pl_ptr;
  865.   }   
  866.   return(location);
  867. }
  868.  
  869. void compile(CHAR *source_ptr, CHAR pbuf[PMAX])
  870. /**C*F*******************************************************************
  871. **                                                                     **
  872. ** SRC-MODULE     : compile                                            **
  873. ** VERSION        : 001                                                **
  874. ** LONG_NAME      : compile                                            **
  875. **                                                                     **
  876. **                                                                     **
  877. ** SPECIFICATION  : this program is based on grep.c by David N. Smith  **
  878. **                  public domain program CUG152.08                    **
  879. ** SUBSYSTEM      : -                                                  **
  880. ** SYSTEM_TYPE    : PCD-3T                                             **
  881. **                                                                     **
  882. ** AUTHOR         : Helene Ballay                                      **
  883. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  884. **                                                                     **
  885. ** DESCRIPTION    : compile() compiles the regular expressions starting**
  886. **                  at source (for further explanation about regular   **
  887. **                  expression see comment just after this header ) it **
  888. **                  stores it in pbuf[] using the patterns already     **
  889. **                  defined (ACHAR, .... ENDPAT) in global declarations**
  890. **                  (see postconditions for further explanation )      **
  891. **                  so that part_match() can use it for finding matches**
  892. **                                                                     **
  893. ** REMARKS        : Note that pbuf[0] contains a flag (bar_flg) which  **
  894. **                  indicates the appearance of '@' in the pattern     **
  895. **                  string.                                            **
  896. **                  Note that the pattern in pbuf[] does not end with  **
  897. **                  ENDPAT but with 0 because ENDPAT can appear several**
  898. **                  times in pbuf[].                                   **
  899. **                                                                     **
  900. ** FUNCTIONS      : store(), printf(), comp_class()                    **
  901. **                                                                     **
  902. ** GLOBALS        :                                                    **
  903. **                                                                     **
  904. ** PARAMETERS     : source_ptr: source_ptr points to the pattern to be **
  905. **                              translated in a regular expression.    **
  906. **                  pbuf[]: pattern template                           **
  907. **                                                                     **
  908. ** PRECONDITIONS  : the string pointed to by source must be written    **
  909. **                  following the notation of regular expression       **
  910. **                  explained below.                                   **
  911. **                                                                     **
  912. ** POSTCONDITIONS : pbuf contains the pattern template, a string of    **
  913. **                  characters, ending with 0, a concatenation of      **
  914. **                  part patterns.                                     **
  915. **                  Each part pattern starts with the constant for the **
  916. **                  number of occurences (MINUS, PLUS, STAR or none if **
  917. **                  it shall match one time). Then the nature of the   **
  918. **                  character to match (BOL, EOL, ANY, NALPHA, VAR),   **
  919. **                  or the nature of the character and the character   **
  920. **                  itself for ACHAR. It ends with ENDPAT. See         **
  921. **                  comp_class() header for explanations about classes.**
  922. **                  If the pattern isn't a regular expression, a       **
  923. **                  warning message will be printed on the standard    **
  924. **                  output and the program stops.                      **
  925. **                                                                     **
  926. **                                                                     **
  927. ***C*F*E****************************************************************/
  928. /*********************************************************************************************
  929. **                                                                                          **
  930. ** The patterns describing the classes of strings to be searched for are written using      **
  931. ** regular expressions with the following notation :                                        **
  932. **                                                                                          **
  933. ** habit     A string of text characters with no operators at all ( ie none of those        **
  934. **          mentionned below) matches the literal string.                                   **
  935. ** fo*       An expression followed by a star matches zero or more occurence of that        **
  936. **          expression : "fo*" matches "f", "fo", "foo" etc.                                **
  937. ** fo-       An expression followed by a minus sign optionally matches the expression :     **
  938. **          "fo-" matches "f" and "fo".                                                     **
  939. ** fo+       An expression followed by a plus sign matches one or more occurence of this    **
  940. **          expression : "fo+" matches "fo", "foo" etc.                                     **
  941. ** ^fo       A circumflex followed by a pattern signifies that the pattern must match at    **
  942. **          the beginning of a line.                                                        **
  943. ** ab$       A dollar sign appended to a pattern indicates that it must match at the end    **
  944. **          of a line.                                                                      **
  945. ** m...y     A period matches any character except new-line : "m...y" matches any five      **
  946. **          letter string starting with m and ending with y.                                **
  947. ** [xyz]     A sequence inside the square brackets matches any character in the sequence    **
  948. **          but no others. If the first character in the string is a ^, it matches any      **
  949. **          character that is not in the string. For example, "[xyz]" matches "xx"          **
  950. **          and "zyx", while "[^xyz]" matches "abc" but not "axb".                          **
  951. ** [a-z]     Ranges within a standard ASCII order are indicated with a hyphen. Note that    **
  952. **          [a-z] matches any alphabetic while [z-a] never matches.                         **
  953. ** :n        A colon followed by a n matches any alphanumeric.                              **
  954. ** :v        A colon followed by a v matches any character allowed in a variable name,      **
  955. **          i.e. lowercase letter, digit or underscore.                                     **
  956. ** \\$       A backslash before an otherwise special character matches it as a literal      **
  957. **          character, e.g. "\\$" matches a dollar sign. To recognize a backslash itself    **
  958. **          use \\\\.                                                                       **
  959. ** @        two regular expressions separated by @ match either a match for the first or    **
  960. **          a match for the second. @ is not allowed to be followed directly by *,+ or -.   **
  961. **                                                                                          **
  962. ** The concatenation of regular expressions is of course a regular expression.              **
  963. **                                                                                          **
  964. *********************************************************************************************/
  965. {
  966.   register CHAR  *p_ptr;                    /* Current pointer in pbuf  */
  967.   register CHAR  c;                         /* Current character.       */
  968.   CHAR  *lp_ptr;                            /* Last pattern pointer.    */
  969.   CHAR  *save_ptr;                          /* Save end of pattern.     */
  970.   CHAR  *s_ptr;                             /* Source string pointer.   */
  971.   CHAR  bar_flg;                            /* is BAR if '@' is in the  */
  972.                                             /* pattern string, 0 other- */
  973.                                             /* wise.                    */
  974.   CHAR **save3_pptr;                        /* *save3_pptr points to    */
  975.                                             /* the last character in    */
  976.                                             /* pbuf[] if it has been    */
  977.                                             /* modified by comp_class().*/
  978.   CHAR   *anchor_ptr;            /* serves just as a defined location  */
  979.                                  /* in memory.                         */
  980.  
  981.  
  982. /*-------- Initialize variables -----------------------------------------------*/
  983.  
  984.   bar_flg  = 0;                                        /* set bar_flg to "no '@'" */
  985.   s_ptr    = source_ptr;                   /* Beginning of the keyword to compile */
  986.   p_ptr    = pbuf;                       /* point at the first place of the array */
  987.   c        = *source_ptr;                                 /* read first character */
  988.   lp_ptr   = NULL;
  989.   save_ptr = NULL;
  990.  
  991.   save3_pptr  = &anchor_ptr;       /* initialize save3_pptr to a defined value.   */
  992.   *save3_pptr = NULL;
  993.  
  994.  
  995.   p_ptr++;                               /* move pattern pointer one place forward*/
  996.                                          /* because pbuf[0] will contain bar_flg. */
  997.  
  998.  
  999. /*   printf(" Pattern = \"%s\"\n", s_ptr); */                /* Debugging code */
  1000.  
  1001. /*-------- Read the source keyword and compile it -----------------------------*/
  1002.  
  1003.   while ( c NEQ '\0' )           /* while all char of pattern are not examined */
  1004.   {
  1005.      s_ptr++;
  1006.  
  1007.      /*----- STAR, PLUS and MINUS are special ---------------------------------*/
  1008.      /*----- (they indicate a number of occurences to match ) -----------------*/
  1009.  
  1010.      if ((c EQ '*') OR (c EQ '+') OR (c EQ '-')) 
  1011.      {
  1012.         store(ENDPAT,pbuf,p_ptr);           /* Store ENDPAT to reserve a place */
  1013.         p_ptr++;                               /* for STAR, PLUS or MINUS flag */ 
  1014.         store(ENDPAT,pbuf,p_ptr);    /* Store ENDPAT at the end of the pattern */
  1015.         p_ptr++;
  1016.         save_ptr = p_ptr;                                  /* Save pattern end */
  1017.         p_ptr++;
  1018.  
  1019.         while (p_ptr > lp_ptr)                   /* Move pattern down one byte */
  1020.         {                                    /* until the beginning of pattern */
  1021.            *p_ptr = p_ptr[-1];               /* lp_ptr MUST NOT BE NULL !!!    */
  1022.            p_ptr--;          
  1023.         }
  1024.         if (c EQ '*')                   /* to store there the appropriate flag */
  1025.         {
  1026.            *p_ptr = STAR;                           /* zero or more occurences */
  1027.         }
  1028.         else if (c EQ '-')
  1029.         {
  1030.            *p_ptr = MINUS;                            /* one or zero occurence */
  1031.         }
  1032.         else
  1033.         { 
  1034.            *p_ptr = PLUS;                             /* one or more occurence */
  1035.         }
  1036.         p_ptr = save_ptr;                          /* Then restore pattern end */
  1037.         c = *s_ptr;
  1038.         continue;                                     /* go back to while loop */           
  1039.      }
  1040.  
  1041.     /*-----  Other than star, plus or minus -----------------------------------*/
  1042.     /*----- ( character or class of character to match ) ----------------------*/
  1043.  
  1044.      lp_ptr = p_ptr;                        /*  Remember start of the pattern  */
  1045.  
  1046.      switch (c)                      /* current char of the regular expression */
  1047.      {
  1048.        case '^':                                        /* Beginning of a line */
  1049.           store(BOL,pbuf,p_ptr);  
  1050.           p_ptr++;
  1051.           break;
  1052.  
  1053.        case '@':                                        /* expression separator*/
  1054.           bar_flg = BAR;                                /* now bar_flg != 0    */
  1055.           store(BAR,pbuf,p_ptr);
  1056.           p_ptr++;
  1057.           break;
  1058.  
  1059.        case '$':                                              /* End of a line */
  1060.           store(EOL,pbuf,p_ptr);                
  1061.           p_ptr++;
  1062.           break;
  1063.  
  1064.        case '.':                              /* Any character except new-line */
  1065.           store(ANY,pbuf,p_ptr);
  1066.           p_ptr++;
  1067.           break;
  1068.  
  1069.        case '[':                                     /* Compile a whole string */
  1070.           /*---- call comp_class() to compile it ------------------------------*/
  1071.           s_ptr=comp_class(source_ptr,s_ptr,pbuf,p_ptr,save3_pptr);
  1072.           p_ptr = *save3_pptr;      /* end of compiled class in pattern template */
  1073.           *save3_pptr = NULL;
  1074.           break;
  1075.  
  1076.        case ':':                                        /* Class of characters */
  1077.           if ( *s_ptr NEQ '\0' )
  1078.           {
  1079.              c = *s_ptr;                   /* read next character from pattern */
  1080.              s_ptr++;
  1081.  
  1082.              switch(tolower(c))                         /* nature of the class */
  1083.              {
  1084.                case 'n':                                   /* any alphanumeric */
  1085.                   store(NALPHA,pbuf,p_ptr);
  1086.                   p_ptr++;
  1087.                   break;
  1088.  
  1089.                case 'v':                             /* any char of a var name */
  1090.                   store(VAR,pbuf,p_ptr);
  1091.                   p_ptr++;
  1092.                   break;
  1093.  
  1094.                default:
  1095.                   printf("Cannot compile %s after ':'.\n", source_ptr);
  1096.                   exit(1);
  1097.                   break;                        /* (a badpat ends the program) */
  1098.              }
  1099.              break;
  1100.           }
  1101.           else
  1102.           {
  1103.              printf("\nBadpat after : in %s \n",source_ptr);
  1104.              exit(1);                            /* (a badpat ends the program) */
  1105.           }
  1106.  
  1107.        case '\\':                                /* Symbol as regular character */
  1108.           if ( *s_ptr  NEQ '\0' )
  1109.           {
  1110.              c = *s_ptr;                                          /* get symbol */
  1111.              s_ptr++;
  1112.              store(ACHAR,pbuf,p_ptr);             /* store it as a regular char */
  1113.              p_ptr++;                             /* you need two buffer places:*/
  1114.              store(c,pbuf,p_ptr);                 /* one for ACHAR and one for  */
  1115.              p_ptr++;                             /* the character itself.      */
  1116.           }
  1117.           break;     
  1118.  
  1119.        default :                                    /* Single regular character */
  1120.           store(ACHAR,pbuf,p_ptr);
  1121.           p_ptr++;
  1122.           store(c,pbuf,p_ptr);
  1123.           p_ptr++;
  1124.           break;
  1125.      }
  1126.      c = *s_ptr;
  1127.   }                                                        /* end of while loop */
  1128.   store(ENDPAT,pbuf,p_ptr);
  1129.   p_ptr++;                      
  1130.   store(0,pbuf,p_ptr);        /* Terminate pattern string with 0 because ENDPAT */
  1131.   p_ptr++;                    /* appears possibly several times in the pattern  */
  1132.   pbuf[0] = bar_flg;          /* Indicate whether '@' has appeared or not.      */
  1133.  
  1134.  
  1135.  /*   for (lp_ptr = pbuf; lp_ptr < p_ptr;) */                 /* debugging code */  
  1136.  /*   {
  1137.       c = *lp_ptr;
  1138.       lp_ptr++;
  1139.       if ( c < ' ')
  1140.          printf(" \\%ld ", c);
  1141.       else
  1142.          printf(" %c ", c);
  1143.     }
  1144.     printf("\n");*/
  1145.  
  1146. }  
  1147. /*-------------- Store in pbuf[] ----------------------------------------------*/
  1148.  
  1149. void store(CHAR op, CHAR pbuf[PMAX], CHAR *p_ptr)
  1150. {
  1151.    if ( p_ptr >= &pbuf[PMAX] )            /* check if there is place in pbuf[] */
  1152.    {
  1153.       printf("\nPattern is too complex to be compiled \n");
  1154.       exit(1);                     /* (a too complex pattern ends the program) */
  1155.    }
  1156.    *p_ptr = op;                                 /* store op in pbuf[] at p_ptr */
  1157. }
  1158.  
  1159. CHAR *comp_class( CHAR *source_ptr, CHAR *cl_st_ptr, CHAR pbuf[], CHAR *p_ptr, CHAR **save3_pptr)
  1160. /**C*F*******************************************************************
  1161. **                                                                     **
  1162. ** SRC-MODULE     : comp_class                                         **                                                   
  1163. ** VERSION        : 001                                                **                                                  
  1164. ** LONG_NAME      : compile class found within []                      **
  1165. **                                                                     **
  1166. **                                                                     **
  1167. ** SPECIFICATION  : This program is based on grep.c by David N. Smith  **
  1168. **                  program of public domain CUG152.08.                **
  1169. ** SUBSYSTEM      : -                                                  **
  1170. ** SYSTEM_TYPE    : PCD-3T                                             **
  1171. **                                                                     **
  1172. ** AUTHOR         : Helene Ballay                                      **
  1173. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  1174. **                                                                     **
  1175. ** DESCRIPTION    : comp_class compiles a class found in source[]      **
  1176. **                  starting at cl_st_ptr in pbuf[] starting at p_ptr. **  
  1177. **                                                                     **
  1178. ** REMARKS        :                                                    **
  1179. **                                                                     **
  1180. ** FUNCTIONS      : store(), exit(), printf(), return()                **
  1181. **                                                                     **
  1182. ** GLOBALS        : *save3_pptr : after comp_class(), will point to    **
  1183. **                                end of class in pattern template     **
  1184. **                                                                     **
  1185. ** PARAMETERS     : source : points at the beginning of the regular    **
  1186. **                           pattern, only used to print it if there   **
  1187. **                           is a problem in it.                       **
  1188. **                  cl_st_ptr : points to the beginning of the class   **
  1189. **                              in the regular expression.             **
  1190. **                  pbuf : contains the pattern template               **
  1191. **                  p_ptr : current position in pbuf given by compile()**
  1192. **                          before compiling the class itself          **
  1193. **                                                                     **
  1194. ** PRECONDITIONS  : the class in the pattern must be written as a      **
  1195. **                  regular expression.                                **
  1196. **                                                                     **
  1197. ** POSTCONDITIONS : the class (or negative class) is stored in pbuf    **
  1198. **                  as a string beginning with CLASS (or NCLASS), then **
  1199. **                  the size of the class (i) and the class itself :   **
  1200. **                  a succession of regular characters and/or range    **
  1201. **                  patterns, ie RANGE, followed by the two limit      **
  1202. **                  characters : for example the result for            **
  1203. **                  "[ad-ftr]" would be : CLASS 6 a RANGE d f t r      **
  1204. **                  *save3_pptr points at the end of the class in the  **
  1205. **                  pattern template.                                  **
  1206. **                                                                     **
  1207. **                                                                     **
  1208. ***C*F*E****************************************************************/
  1209. {
  1210.   register CHAR *s_ptr;           /* source pointer                     */
  1211.   register CHAR c;                /* current character                  */
  1212.   CHAR *beg_ptr;                  /* beginning of the pattern within [] */
  1213.   CHAR temp;                      /* temporaire                         */
  1214.   LONG i;                         /* size of class                      */
  1215.  
  1216.   /*-------- Initialize variables ---------------------------------------------*/
  1217.  
  1218.   s_ptr   = cl_st_ptr;                                 /* point to class start */
  1219.   beg_ptr = p_ptr;                     /* point to the current pointer in pbuf */
  1220.   i       = 0;                                            /* size of the class */
  1221.   c       = *s_ptr;
  1222.  
  1223.   /*-------- Store CLASS or NCLASS --------------------------------------------*/
  1224.  
  1225.   temp = CLASS;
  1226.   if ( c EQ '^' )                       /* if pattern [^.... ie negative class */
  1227.   {
  1228.      temp  = NCLASS ;
  1229.      s_ptr = s_ptr + 1;        /* point to the first char oif the class itself */
  1230.   } 
  1231.   store(temp,pbuf,p_ptr);                            /* store appropriate flag */
  1232.   p_ptr++;                                                /* (CLASS or NCLASS) */
  1233.  
  1234.   beg_ptr = p_ptr;                                  /* point to start of class */
  1235.   store(0,pbuf,p_ptr);                                           /* Byte count */
  1236.   p_ptr++;
  1237.   
  1238.   c = *s_ptr;                       /* read the first char of the class itself */
  1239.  
  1240.   /*-------- Compile the class itself ------------------------------------------*/
  1241.   
  1242.   while ((c NEQ '\0') AND (c NEQ ']'))            /* while not end of the class */
  1243.   {
  1244.      s_ptr = s_ptr + 1;     
  1245.      if ( c EQ '\\')              /* Store special quoted char as a normal char */
  1246.      {
  1247.         c = *s_ptr;
  1248.         s_ptr++;        
  1249.         if (c EQ '\0')                                   /* gotta get something */
  1250.         {
  1251.             /* message d'erreur */;
  1252.             exit(1);                                   /* (badpat ends program) */
  1253.         }
  1254.         else
  1255.         {
  1256.            store(c,pbuf,p_ptr);
  1257.            p_ptr++;
  1258.         }
  1259.      }
  1260.      else if ( (c EQ '-') AND ((p_ptr - beg_ptr)> 1) 
  1261.                           AND ( *s_ptr NEQ ']' ) AND ( *s_ptr NEQ '\0'))
  1262.      {
  1263.         c = p_ptr[-1];                                           /* Range start */
  1264.         p_ptr[-1] = RANGE;                                      /* Range signal */
  1265.         store(c,pbuf, p_ptr);                                 /* Re-store start */
  1266.         p_ptr++;
  1267.         c = *s_ptr;                                         /* Get end char and */
  1268.         s_ptr++;
  1269.         store(c,pbuf,p_ptr);                                        /* store it */
  1270.         p_ptr++; 
  1271.      }
  1272.      else                                                        /* Normal char */
  1273.      {
  1274.         store(c,pbuf,p_ptr);                                        /* store it */
  1275.         p_ptr++;
  1276.      } 
  1277.      c = *s_ptr;                                              /* Read next char */
  1278.   }
  1279.   s_ptr++;
  1280.   i = p_ptr - beg_ptr;                                     /* size of the class */
  1281.  
  1282.   if (( c NEQ ']' ) OR (i >= 256) OR (i EQ 0))
  1283.   {
  1284.      printf("Bad class in the pattern : %s\n",source_ptr);
  1285.      exit(1);                                          /* (badpat ends program) */        
  1286.   }
  1287.   *beg_ptr = (CHAR)i;                                /* store size of the class */
  1288.   *save3_pptr = p_ptr;                                  /* end of class in pbuf[] */
  1289.   return(s_ptr);                /* return a pointer to the next char to examine */
  1290. }
  1291.  
  1292. CHAR *part_match(CHAR *start_ptr, CHAR pattern[], LONG *l_ptr, CHAR **save2_pptr, CHAR mode)
  1293. /**C*F*******************************************************************
  1294. **                                                                     **
  1295. ** SRC-MODULE     : part_match                                         **
  1296. ** VERSION        : 001                                                **
  1297. ** LONG_NAME      : partial match                                      **
  1298. **                                                                     **
  1299. **                                                                     **
  1300. ** SPECIFICATION  : This program is based on grep.c by David N. Smith  **
  1301. **                  public domain CUG152.08                            **
  1302. ** SUBSYSTEM      : -                                                  **
  1303. ** SYSTEM_TYPE    : PCD-3T                                             **
  1304. **                                                                     **
  1305. ** AUTHOR         : Helene Ballay                                      **
  1306. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  1307. **                                                                     **
  1308. ** DESCRIPTION    : part_match() tries to find a match of the pattern  **
  1309. **                  starting exactly at start, skipping comments and   **
  1310. **                  quoted text, and saving the current line in *line. **
  1311. **                  The end of match will be returned, NULL if none.   **
  1312. **                                                                     **
  1313. ** REMARKS        : -                                                  **
  1314. **                                                                     **
  1315. ** FUNCTIONS      : return(), part_match(), exit(), printf()           **
  1316. **                                                                     **
  1317. ** GLOBALS        : *save2_pptr : points eventually to end of text     **
  1318. **                                skipped by part_match() ie text to   **
  1319. **                                skip for find_keyword()              **
  1320. **                                                                     **
  1321. ** PARAMETERS     : start_ptr : place where the match should start     **
  1322. **                  pattern : points to beginning of pattern template  **
  1323. **                            to match                                 **
  1324. **                  l_ptr : contains the current line. As a pointer,   **
  1325. **                          it keeps this value even after return.     **
  1326. **                  mode:  0 --> it is assumed that the pattern in     **
  1327. **                               pattern[] starts in location 0.       **
  1328. **                         1 --> pattern start in location 1. Location **
  1329. **                               zero is used by bar_flag. (See also   **
  1330. **                               compile()).                           **
  1331. **                                                                     **
  1332. ** PRECONDITIONS  : the comments and quoted text must be correct       **
  1333. **                  ( braces_etc succeeded )                           **
  1334. **                                                                     **
  1335. ** POSTCONDITIONS : returns the end of match found, NULL if none, and  **
  1336. **                  saves in *save2_pptr the end of the last text      **
  1337. **                  skipped over (NULL if none).                       **
  1338. **                                                                     **
  1339. **                                                                     **
  1340. ***C*F*E****************************************************************/
  1341. {
  1342.   register CHAR   *p_ptr;             /* current pattern pointer       */
  1343.   register CHAR   *s_ptr;             /* current source pointer        */
  1344.   CHAR   c;                           /* current character             */ 
  1345.   LONG   op;                          /* pattern operation             */        
  1346.   CHAR   *end_ptr;                    /* end for STAR  and MINUS match */
  1347.   CHAR   *beg_ptr;                    /* start for STAR match          */
  1348.   LONG   n;                           /* class counter                 */
  1349.   CHAR   *truc_ptr;                   /* save end of skipped text in   */
  1350.                                       /* order to avoid going through  */
  1351.                                       /* it more times than necessary  */
  1352.   CHAR   *save_ptr;
  1353.   CHAR   match_flg;                   /* 1 on match, 0 if no match.    */
  1354.   CHAR   bar_flg;                     /* >0 if BAR in pattern, 0 other-*/
  1355.                                       /* wise.                         */
  1356.   CHAR  **help_pptr;
  1357.   LONG   line;
  1358.  
  1359. /*-------- Initialize variables ---------------------------------------------- */
  1360.  
  1361.   s_ptr     = start_ptr ;             /* start of the search in source_file[]  */
  1362.   p_ptr     = pattern ;               /* start of the partial pattern to match */
  1363.   help_pptr = save2_pptr;
  1364.   line      = *l_ptr;
  1365.   bar_flg   = 0;                      /* set bar_flg to "no BAR".              */
  1366.  
  1367.   if (mode EQ 0)
  1368.     {
  1369.       op    = *p_ptr;                 /* points to first pattern character     */
  1370.     }
  1371.   else
  1372.     {
  1373.       bar_flg = *p_ptr;               /* bar_flg indicates presence of BAR.    */
  1374.       p_ptr++;                        /* switch to pattern[1].                 */
  1375.       op      = *p_ptr;               /* points to first pattern character     */
  1376.     }
  1377.  
  1378.   n     = 0;                                                    /* empty class */
  1379.   c     = 'a';                                                  /* dummy value */
  1380.   end_ptr   = NULL;
  1381.   beg_ptr   = NULL;
  1382.   truc_ptr  = NULL;
  1383.   match_flg = 1;                      /* set flag to "match" (no early return) */
  1384.  
  1385. /*-------------                                         ---------------------- */
  1386.  
  1387.   while ((op NEQ ENDPAT) AND (match_flg EQ 1) AND (op NEQ BAR))
  1388.   {                                             /* while end of pattern temp-  */
  1389.                                                 /* late not reached and there  */
  1390.                                                 /* is still a match.           */
  1391.  
  1392.      /*-------- Skip over comment -------------------------------------------- */
  1393.  
  1394.      if ((*s_ptr EQ '/') AND (*(s_ptr + 1) EQ '*'))
  1395.      {
  1396.         do                                                    /* go through it */
  1397.         {
  1398.              s_ptr++;
  1399.              if ( *s_ptr EQ '\0' )           /* eventually incrementing n_line */
  1400.                 (*l_ptr)++;
  1401.  
  1402.         }while ((*(s_ptr -1) NEQ '*') OR ( *s_ptr NEQ '/'));   /* 'til its end */
  1403.             
  1404.         *save2_pptr = s_ptr;                            /* save end of comment */
  1405.         s_ptr++;
  1406.         continue;                                   /* (go back to while loop) */ 
  1407.      }
  1408.  
  1409.      /*-------- Skip over quoted text ---------------------------------------- */
  1410.      /*------ ('"' is no start and \" is no end of quoted text )-------------- */
  1411.  
  1412.      if ( (*s_ptr EQ '"') AND ((*(s_ptr-1) NEQ '\'') OR (*(s_ptr +1) NEQ '\'')) )
  1413.      {
  1414.          do                                                   /* go through it */
  1415.          {
  1416.             s_ptr++;                                  
  1417.  
  1418.             if (*s_ptr EQ '\0')              /* incrementing eventually n_line */
  1419.                (*l_ptr)++;
  1420.  
  1421.          } while ((*s_ptr NEQ '"') OR (*(s_ptr-1) EQ '\\'));
  1422.  
  1423.          *save2_pptr = s_ptr;                       /* save end of quoted text */
  1424.          s_ptr++;
  1425.          continue;                                  /* (go back to while loop) */
  1426.      }
  1427.  
  1428.      /*-------- Examine pattern and source file ------------------------------ */
  1429.  
  1430.      p_ptr++;                                  /* point to next pattern symbol */
  1431.  
  1432.      switch(op)                                   /* current pattern operation */
  1433.      {
  1434.        case ACHAR:                                              /* a character */
  1435.           c = *s_ptr ;
  1436.           s_ptr++;
  1437.           if ( c NEQ *p_ptr )
  1438.             {
  1439.              match_flg = 0;
  1440.             }
  1441.           else
  1442.             {
  1443.               p_ptr++;                   /* one location further because ACHAR */
  1444.             }                            /* needs two buffer places.           */
  1445.           break;
  1446.        
  1447.        case BOL:                                        /* beginning of a line */
  1448.           if ( *(s_ptr - 1) NEQ '\0' )
  1449.              match_flg = 0;
  1450.           break;
  1451.  
  1452.        case EOL:                                              /* end of a line */
  1453.           if ( *s_ptr NEQ '\0' )
  1454.              match_flg = 0;
  1455.           break;
  1456.  
  1457.        case ANY:                                              /* any character */
  1458.           c = *s_ptr ;
  1459.           s_ptr++; 
  1460.           if ( c EQ '\0' )
  1461.              match_flg = 0;
  1462.           break;
  1463.  
  1464.        case NALPHA:                                        /* any alphanumeric */
  1465.           c = (CHAR)tolower( *s_ptr );
  1466.           s_ptr++;
  1467.           if ((c >= 'a') AND ( c <= 'z' ))
  1468.              break;
  1469.           else if (( c < '0' ) OR ( c > '9'))
  1470.              match_flg = 0;
  1471.           break; 
  1472.  
  1473.        case VAR:                                /* any char of a var name, i.e.*/
  1474.           c = *s_ptr;                           /* alphanumeric or underscore  */
  1475.           s_ptr++;
  1476.           if ( (c NEQ '_') AND ((c <'a') OR (c >'z')) AND ((c <'0') OR (c >'9')) )
  1477.               match_flg = 0;
  1478.           break;
  1479.  
  1480.        case CLASS:                        /* any member of the following class */
  1481.        case NCLASS:                    /* any nonmember of the following class */
  1482.           c =  *s_ptr;
  1483.           if ( *s_ptr NEQ '\0')      /* read next char if line is not finished */
  1484.               s_ptr++;
  1485.           n = ( *p_ptr);                                  /* size of the class */
  1486.           p_ptr++;                           /* first char of the class itself */
  1487.  
  1488.           do                                              /* Explore the class */
  1489.           {
  1490.             if ( *p_ptr EQ RANGE )          /* If range of char in the pattern */
  1491.             {
  1492.                p_ptr = p_ptr + 3;                   /* points after its limits */ 
  1493.                n     = n - 2;                  /* limits = two characters read */
  1494.                if ((c >= p_ptr[-2] ) AND (c <= p_ptr[-1] ))   /* got a match ? */
  1495.                   break;
  1496.             }
  1497.             else                /* If the char in the pattern isn`t in a range */
  1498.             {
  1499.                if (c EQ *p_ptr)                               /* got a match ? */
  1500.                {                           
  1501.                   p_ptr++;
  1502.                   break;
  1503.                }
  1504.                p_ptr++;
  1505.             }
  1506.             n = n - 1; 
  1507.           } while ( n > 1 );                       /* class non fully examined */
  1508.  
  1509.           if (((op EQ CLASS) AND (n <= 1)) OR ((op EQ NCLASS) AND (n > 1)))
  1510.             {
  1511.               match_flg = 0;
  1512.             }
  1513.           else if (op EQ CLASS)            /* points in the pattern after the class */
  1514.             {                              /*(already done by a sucessfull nonclass)*/
  1515.               p_ptr = p_ptr + n - 2;
  1516.             }
  1517.           break;
  1518.  
  1519. /*-----MINUS, PLUS and STAR require recursive programming by calling-----------*/
  1520. /*-----part_match() itself.----------------------------------------------------*/
  1521.  
  1522.        case MINUS:                                    /* Zero or one occurence */
  1523.           end_ptr = part_match(s_ptr,p_ptr,l_ptr,save2_pptr,0); /* look for a match */
  1524.           while ( *p_ptr NEQ ENDPAT)
  1525.              p_ptr++;                                     /* skip over pattern */
  1526.           if (end_ptr NEQ NULL )                              /* got a match ? */
  1527.              s_ptr = end_ptr;                            /* yes, update string */
  1528.           break;                                            /* always succeeds */
  1529.  
  1530.        case PLUS:                                            /* One or more... */
  1531.           s_ptr = part_match(s_ptr,p_ptr,l_ptr,save2_pptr,0);
  1532.           if (s_ptr EQ NULL)
  1533.             {
  1534.               match_flg = 0;                             /* gotta have a match */
  1535.               break;
  1536.             }
  1537.  
  1538.        /* the absence of break at the end of this case PLUS is deliberate, as  */
  1539.        /* one or more occurence is the same as one followed by zero or more !  */     
  1540.  
  1541.        case STAR:                                           /* Zero or more... */
  1542.           beg_ptr = s_ptr;                              /* remember line start */
  1543.           save_ptr = *save2_pptr;
  1544.  
  1545.           end_ptr = part_match(s_ptr,p_ptr,l_ptr,save2_pptr,0); /* look for match */
  1546.  
  1547.           /*---- First eliminate all the char defined by the STAR pattern -----
  1548.             ---- by scanning along source_file[] current line and calling -----
  1549.             ---- part_match() until a mismatch is found. Then try to match ----
  1550.             ---- the rest of the template against the rest of text string. ----
  1551.             ---- If part_match() fails to match the tail, go backwards through 
  1552.             ---- the characters just processed, while trying to match the -----
  1553.             ---- trailing string against the rest of the pattern template. ----
  1554.             ---- This is necessary because the character following the 'STAR'--
  1555.             ---- could have been included in the 'STAR' pattern itself. -------
  1556.             ---- ( For example : [a-d]*b : b will be absorbed by the first ----
  1557.             ---- scan, it is necessary to go backwards eg in aaaaabccccc ) ----*/
  1558.  
  1559.           /*------------ get longest match ------------------------------------*/
  1560.  
  1561.           while ((end_ptr NEQ NULL) AND (*end_ptr NEQ '\0'))
  1562.           {
  1563.             *save2_pptr = NULL; /* ???????????? */
  1564.             s_ptr = end_ptr;
  1565.             end_ptr = part_match(s_ptr,p_ptr,l_ptr,save2_pptr,0);  /* next match */
  1566.           } 
  1567.           if (*save2_pptr NEQ NULL )                         /* If text skipped, */
  1568.           {
  1569.             s_ptr = *save2_pptr + 1;                          /* points after it */
  1570.           }
  1571.  
  1572.           /*------------ skip over pattern ------------------------------------*/
  1573.  
  1574.           while ( *(p_ptr - 1) NEQ ENDPAT)
  1575.           {
  1576.             p_ptr++;
  1577.           }
  1578.  
  1579.           /*------------ try to match rest ------------------------------------*/
  1580.                         
  1581.           while (( s_ptr >= beg_ptr ) AND ( *s_ptr NEQ '\0'))
  1582.           {
  1583.             end_ptr = part_match(s_ptr,p_ptr,l_ptr,save2_pptr,0); /* look for match */
  1584.  
  1585.             if ( end_ptr NEQ NULL )                          /* if match found */
  1586.                return(end_ptr);
  1587.  
  1588.             if ((*save2_pptr NEQ NULL) AND (s_ptr NEQ beg_ptr) AND (*save2_pptr NEQ truc_ptr) )
  1589.             {
  1590.                s_ptr = *save2_pptr + 1;
  1591.             }    
  1592.             s_ptr--;                                     /* nope,go backwards  */
  1593.  
  1594.             if (s_ptr <= beg_ptr)                     /* (if beginning of star */
  1595.                continue;              /* match reached, go back to while loop) */
  1596.  
  1597.             /*-------------- skipping over comment ----------------------------*/
  1598.             if (( *s_ptr EQ '/') AND ( *(s_ptr - 1) EQ '*'))
  1599.             {
  1600.                truc_ptr = s_ptr;                        /* save end of comment */
  1601.                do
  1602.                {
  1603.                    s_ptr--;                           /* go backwards one char */
  1604.                    if (*s_ptr EQ '\0')         /* ev. decrementing line number */
  1605.                       (*l_ptr)-- ;
  1606.                }while ((*s_ptr NEQ '*' ) OR ( *(s_ptr - 1) NEQ '/'));
  1607.                s_ptr = s_ptr - 2;
  1608.                continue;                              /* go back to while loop */
  1609.             }
  1610.             /*--------------- skipping over quoted text -----------------------*/
  1611.             if (( *s_ptr EQ '"') 
  1612.                  AND (( *(s_ptr-1) NEQ '\'') OR ( *(s_ptr+1) NEQ '\'')) )
  1613.             {
  1614.                truc_ptr = s_ptr;                    /* save end of quoted text */
  1615.                do 
  1616.                {
  1617.                   s_ptr--;                                /* go one char back */ 
  1618.                   if ( *s_ptr EQ '\0')       /* ev. decrementing line counter */ 
  1619.                       (*l_ptr)--;
  1620.                }while (( *s_ptr NEQ '"') OR (*(s_ptr -1) EQ '\\'));
  1621.                s_ptr = s_ptr - 2;
  1622.             }
  1623.           }                                               /* end of while loop */
  1624.           if ((*save2_pptr EQ NULL) AND (save_ptr NEQ NULL))
  1625.              *save2_pptr = save_ptr;
  1626.           match_flg = 0;                               /* nothing else worked  */
  1627.           break;
  1628.  
  1629.        default:
  1630.           printf("Bad op code %ld at place %ld\n",op,(p_ptr - 1 - pattern));
  1631.           exit(1);                                /* (badpat ends the program) */
  1632.           break;
  1633.       
  1634.      }/* End of switch */
  1635.  
  1636.      op = *p_ptr;                             /* next operation of the pattern */
  1637.  
  1638.      if ((match_flg EQ 0) AND (bar_flg > 0))  /* if there was no match and     */
  1639.        {                                      /* there is BAR in pattern.      */
  1640.          while ((op NEQ 0) AND (op NEQ BAR))        /* then search for BAR to  */
  1641.            {                                        /* find alternative pattern*/
  1642.              p_ptr++;                               /* to match.               */
  1643.              op = *p_ptr;
  1644.            }
  1645.  
  1646.          if (op EQ BAR)                 /* if there is an alternative pattern */
  1647.            {                            /* give part_match() another chance.  */
  1648.              match_flg  = 1;            /* if op EQ ENDPAT, however, match_flg*/
  1649.              s_ptr      = start_ptr;    /* will remain 0 and stop the while   */
  1650.              n          = 0;            /* loop. Reinitialize now!            */
  1651.              end_ptr    = NULL;
  1652.              beg_ptr    = NULL;
  1653.              truc_ptr   = NULL;
  1654.              save2_pptr = help_pptr;
  1655.              *l_ptr     = line;
  1656.              p_ptr++;
  1657.              op = *p_ptr;               /* first pattern character of alter-  */
  1658.            }                            /* native pattern.                    */
  1659.        }
  1660.   }/* End of while */
  1661.  
  1662. /*-----Finally check whether there has been any match-------------------------*/
  1663.  
  1664.   if (match_flg EQ 1)
  1665.     {
  1666.       return(s_ptr);  /* return pointer to end of match found in source_file[].*/
  1667.     }
  1668.   else
  1669.     {
  1670.       return(NULL);
  1671.     }
  1672. }
  1673.  
  1674.  
  1675.  
  1676. void forbidden_keyword(CHAR source_file[], LONG nb_line, FILE *fout_ptr) 
  1677. /**C*F*******************************************************************
  1678. **                                                                     **
  1679. ** SRC-MODULE     : forbidden_keyword                                  **
  1680. ** VERSION        : 001                                                **
  1681. ** LONG_NAME      : forbidden_keyword                                  **
  1682. **                                                                     **
  1683. ** SPECIFICATION  : C coding conventions                               **
  1684. ** SUBSYSTEM      : -                                                  **
  1685. ** SYSTEM_TYPE    : PCD-3T                                             **
  1686. **                                                                     **
  1687. ** AUTHOR         : Helene Ballay                                      **
  1688. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  1689. **                                                                     **
  1690. ** DESCRIPTION    : forbidden_keyword() looks for expressions that are **
  1691. **                  not allowed in source_file[]. It calls             **
  1692. **                  find_keyword() for each pattern already compiled   **
  1693. **                  and prints a warning in fout_ptr for each match    **
  1694. **                  found.                                             **
  1695. **                                                                     **
  1696. ** REMARKS        : the explicit list of forbidden keywords is in      **
  1697. **                  *false_ptr[].                                      **
  1698. **                                                                     **
  1699. ** FUNCTIONS      : strncmp(), compile(), find_keyword(), fprintf()    **
  1700. **                                                                     **
  1701. ** GLOBALS        : source_file[]                                      **
  1702. **                                                                     **
  1703. ** PARAMETERS     : nb_line :number of lines to examine in source_file **
  1704. **                  fout_ptr:points to output file                     **
  1705. **                                                                     **
  1706. ** PRECONDITIONS  : source_file[] must contain an ASCII file with all  **
  1707. **                  '\n' replaced by '\0'.                             **
  1708. **                                                                     **
  1709. ** POSTCONDITIONS : file pointed to by fout_ptr will contain results   **
  1710. **                  of checking process.                               **
  1711. **                                                                     **
  1712. **                                                                     **
  1713. ***C*F*E****************************************************************/
  1714.   CHAR   pbuf[PMAX];                    /* compiled pattern            */
  1715.   T_LOCA loca_1;                        /* current location of keyword */
  1716.   LONG   i;                             /* universal counter           */
  1717.   CHAR   *st_ptr;                       /* start of the search         */
  1718.   LONG   ligne;                         /* start of the search         */
  1719.  
  1720. /*---------- Initialize variables ---------------------------------------------*/
  1721.  
  1722.   CHAR *pattern_ptr[] =                /* patterns to compile (see compile() ) */ 
  1723.   {                                    /* to find the non-allowed expressions  */
  1724.           "&&","||","==","!=",
  1725.           "extern","static",
  1726.           " long ","^long ",                /* type name shall be preceeded by */ 
  1727.           " short ","^short ",              /* space or at the beginning of a  */
  1728.           " char ","^char ",                /* line                            */
  1729.           " int ","^int ",
  1730.           " \\**l[ ;]",                                /* l or *l or *l; or l; */
  1731.           "goto ",
  1732.           "=\\-","\\-\\-\\-",
  1733.           "\\+=","\\*=",
  1734.           "\\-=","/=",
  1735.           "\\*( *\\*(",
  1736.        /* "\\*\\*", */
  1737.           " _:n",
  1738.           ":n__+:n",
  1739.           "\t",
  1740.           "the_end"  
  1741.   };
  1742.   CHAR *false_ptr[] =                      /* explicit non-allowed expressions */
  1743.   {                                                   /* used in */
  1744.           "&&","||","==","!=",                    /* warning messages */
  1745.           "extern","static",
  1746.           "unsigned or (signed) long","unsigned or (signed) long",
  1747.           "unsigned or (signed) short","unsigned or (signed) short",
  1748.           "unsigned or (signed) char","unsigned or (signed) char",
  1749.           "int","int",
  1750.           "l (easily confused with 1)",
  1751.           "goto",
  1752.           "=-","---",
  1753.           "+=","*=",
  1754.           "-=","/=",
  1755.           "*(*(pointer_name... ))",
  1756.        /* "pointer to pointer",  */
  1757.           "_name",
  1758.           "variable__name",
  1759.           "tabulation"
  1760.   };
  1761.   CHAR *right_ptr[] =                          /* explicit correct expressions */
  1762.   {                                                      /* used in */
  1763.           "AND","OR","EQ","NEQ",                     /* warning messages */
  1764.           "IMPORT","LOCAL",
  1765.           "ULONG or LONG","ULONG or LONG",
  1766.           "USHORT or SHORT","USHORT or SHORT",
  1767.           "UCHAR or CHAR","UCHAR or CHAR",
  1768.           "either BITFIELD if unsigned or SHORT or LONG if signed","either BITFIELD if unsigned or SHORT or LONG if signed",
  1769.           "any other variable name",
  1770.           "statements like return, break and continue",
  1771.           " = - ( "," parentheses or even better several statements ",
  1772.           "the non-shorthand notation","the non-shortand notation", 
  1773.           "an explicit notation","an explicit notation",
  1774.           "index notation for ease of readability",
  1775.        /* "index notation for ease of readability",  */
  1776.           "a name that doesn't start with an underscore",
  1777.           "a name that doesn't contain a double underscore",
  1778.           "spaces"
  1779.   };
  1780.   ligne   = 1;
  1781.   st_ptr  = source_file;                                         /* first line */
  1782.  
  1783. /*---------- Look for each non correct expression -----------------------------*/
  1784.  
  1785.   for ( i=0; strncmp(pattern_ptr[i],"the_end",7) NEQ 0 ; i++)
  1786.   {
  1787.     /*--- compile pattern of non-allowed expression ---------------------------*/
  1788.     /*--- and store it in pbuf[]-----------------------------------------------*/
  1789.  
  1790.     compile(pattern_ptr[i],pbuf);
  1791.  
  1792.     /*--- try to find not allowed keyword -------------------------------------*/
  1793.  
  1794.     loca_1 = find_keyword(st_ptr, ligne, nb_line, pbuf);        /* first match */
  1795.  
  1796.     while ((loca_1.line_no)> 0)                           /* while match found */
  1797.     {
  1798.       fprintf(fout_ptr,"\nWARNING (line %4ld) : %s shouldn't be used,", loca_1.line_no, false_ptr[i]);
  1799.       fprintf(fout_ptr," please use %s instead.",right_ptr[i]);
  1800.       st_ptr = loca_1.end_ptr;
  1801.       ligne  = loca_1.line_no;
  1802.       loca_1 = find_keyword(st_ptr, ligne, nb_line, pbuf);       /* next match */
  1803.     }
  1804.     st_ptr = source_file;
  1805.     ligne  = 1;                       /* go back to beginning of source_file[] */
  1806.   } 
  1807. }
  1808. void right_place(CHAR source_file[], LONG nb_line, FILE *fout_ptr) 
  1809. /**C*F*******************************************************************
  1810. **                                                                     **
  1811. ** SRC-MODULE     : right_place                                        **
  1812. ** VERSION        : 001                                                **
  1813. ** LONG_NAME      : right place                                        **
  1814. **                                                                     **
  1815. **                                                                     **
  1816. ** SPECIFICATION  : C coding conventions                               **
  1817. ** SUBSYSTEM      : -                                                  **
  1818. ** SYSTEM_TYPE    : PCD-3T                                             **
  1819. **                                                                     **
  1820. ** AUTHOR         : Helene Ballay                                      **
  1821. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  1822. **                                                                     **
  1823. ** DESCRIPTION    : right_place looks for expressions that contain     **
  1824. **                  keywords at a wrong place in source_file[] and     **
  1825. **                  prints a warning in fout_ptr for each match found. **
  1826. **                                                                     **
  1827. ** REMARKS        : explicit keywords are in false_ptr[] and their     **
  1828. **                  corresponding right places are in right_ptr[].     **
  1829. **                                                                     **
  1830. ** FUNCTIONS      : strncmp(), compile(), find_keyword(), fprintf()    **
  1831. **                                                                     **
  1832. ** GLOBALS        : source_file                                        **
  1833. **                                                                     **
  1834. ** PARAMETERS     : nb_line :number of lines to examine in source_file **
  1835. **                  fout_ptr:points to output file                     **
  1836. **                                                                     **
  1837. ** PRECONDITIONS  : source_file[] must contain an ASCII file with all  **
  1838. **                  '\n' replaced by '\0'.                             **
  1839. **                                                                     **
  1840. ** POSTCONDITIONS : file pointed to by fout_ptr will contain results   **
  1841. **                  of checking process.                               **
  1842. **                                                                     **
  1843. **                                                                     **
  1844. ***C*F*E****************************************************************/
  1845.   CHAR   pbuf[PMAX];                    /* compiled pattern            */
  1846.   CHAR   pbufbis[PMAX];
  1847.   LONG   ligne;                         /* ligne the search begins at  */
  1848.   T_LOCA loca_1;                        /* current location of keyword */
  1849.   T_LOCA loca_2;
  1850.   LONG   i;                             /* universal counter           */
  1851.   CHAR   *st_ptr;                       /* place to start the search   */
  1852.  
  1853.  
  1854. /*---------- Initialize variables ---------------------------------------------*/
  1855.   
  1856.   CHAR *pattern_ptr[] =                 /* patterns to look for                */
  1857.   {                                     /* once compiled in pbuf               */
  1858.           "[^ ] *typedef",
  1859.           "[^ ] *EXPORT",
  1860.           "[^ ] *IMPORT",
  1861.           "[^ ] *LOCAL",
  1862.           "[^ ] *auto",
  1863.           "[^ ] *register",
  1864.           "[^ ] *volatile",
  1865.           "[^ \t'] *{",      /* anything except blank, tab and ' is forbidden. */
  1866.           "{ *[^ \t']",
  1867.           "[^ \t'] *}",
  1868.           "} *[^ \t';]",
  1869.           "; *[¡-²!-~]",                   /* ASCII class : anything but space */       
  1870.           "the_end"
  1871.   };
  1872.   CHAR *pat_bis[] =                        /* Exception patterns */
  1873.   {
  1874.           " *enum *",
  1875.           " *enum *",
  1876.           " *enum *",
  1877.           " *while *",
  1878.           " *for *("
  1879.   };
  1880.   CHAR *false_ptr[] =                   /* explicit keyword at a wrong place   */
  1881.   {                                     /* used in warning message             */   
  1882.           "typedef",
  1883.           "EXPORT",
  1884.           "IMPORT",
  1885.           "LOCAL",
  1886.           "auto",
  1887.           "register",
  1888.           "volatile",
  1889.           "{",
  1890.           "{",
  1891.           "}",
  1892.           "}",
  1893.           ";"
  1894.   };
  1895.   CHAR *right_ptr[] =                   /* explicit right place of each keyword */
  1896.   {                                                         /* used in warnings */
  1897.           "at the beginning of the declaration statement",
  1898.           "at the beginning of the declaration statement",
  1899.           "at the beginning of the declaration statement",
  1900.           "at the beginning of the declaration statement",
  1901.           "at the beginning of the declaration statement",
  1902.           "at the beginning of the declaration statement",
  1903.           "at the beginning of the declaration statement",
  1904.           "in a separate line",
  1905.           "in a separate line",
  1906.           "in a separate line",
  1907.           "in a separate line",
  1908.           "at the end of the line. There should be no more than one executable statement in a single line"
  1909.   };           
  1910.   ligne  = 1;
  1911.   st_ptr = source_file;                                          /* first line */
  1912.   i      = 0;
  1913.  
  1914. /*---------- Look for each expression at a wrong place ------------------------*/
  1915.  
  1916.   for ( i=0; strncmp(pattern_ptr[i],"the_end",7) NEQ 0 ; i++ )
  1917.   {
  1918.  
  1919.      /*----- compile each pattern with the expression at a wrong place --------*/
  1920.  
  1921.      compile(pattern_ptr[i],pbuf);
  1922.      if (i>6)
  1923.      {
  1924.          compile(pat_bis[i-7],pbufbis);
  1925.      }
  1926.      /*----- try to find keyword at a wrong place -----------------------------*/
  1927.  
  1928.      loca_1 = find_keyword(st_ptr, ligne, nb_line, pbuf);
  1929.      
  1930.      while ((loca_1.line_no)> 0)                          /* while match found */
  1931.      {
  1932.        if (i>6)                                     /* if there are exceptions */
  1933.        {
  1934.           if (loca_1.line_no > 1)            /* if match is not in first line. */
  1935.             {
  1936.               while ( *(st_ptr-1) NEQ '\0' )      /* go back to start of line. */
  1937.                {
  1938.                  st_ptr--;
  1939.                }
  1940.             }
  1941.           else                    /* if match in first line go back to start   */
  1942.             {                     /* of file (There's no \0 at the beginning!).*/
  1943.               st_ptr = source_file;
  1944.             }
  1945.  
  1946.  
  1947.           do
  1948.           {
  1949.              loca_2 = find_keyword(st_ptr, ligne, nb_line, pbufbis);
  1950.              st_ptr = loca_2.end_ptr;
  1951.              ligne  = loca_2.line_no;
  1952.           }while( (loca_2.line_no NEQ 0) AND (loca_2.line_no < loca_1.line_no));
  1953.        }
  1954.        if ((i<=6) OR ((i>6) AND (loca_2.line_no NEQ loca_1.line_no)) ) 
  1955.        {
  1956.           fprintf(fout_ptr,"\nWARNING (line %4ld) : the symbol %s ", loca_1.line_no, false_ptr[i]);
  1957.           fprintf(fout_ptr,"should stand %s.", right_ptr[i]);
  1958.        }
  1959.        st_ptr = loca_1.end_ptr;
  1960.        ligne  = loca_1.line_no;
  1961.        loca_1 = find_keyword(st_ptr, ligne, nb_line, pbuf);     /* next match */
  1962.      }
  1963.      st_ptr = source_file;
  1964.      ligne     = 1;                  /* go back to beginning of source_file[] */
  1965.   } 
  1966. }
  1967.  
  1968. void user_type_name(CHAR source_file[], LONG nb_line, FILE *fout_ptr) 
  1969. /**C*F*******************************************************************
  1970. **                                                                     **
  1971. ** SRC-MODULE     : user_type_name                                     **
  1972. ** VERSION        : 001                                                **
  1973. ** LONG_NAME      : user type name                                     **
  1974. **                                                                     **
  1975. **                                                                     **
  1976. ** SPECIFICATION  : C coding conventions                               **
  1977. ** SUBSYSTEM      : -                                                  **
  1978. ** SYSTEM_TYPE    : PCD-3T                                             **
  1979. **                                                                     **
  1980. ** AUTHOR         : Helene Ballay                                      **
  1981. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  1982. **                                                                     **
  1983. ** DESCRIPTION    : user_type_name looks for declaration of user type  **
  1984. **                  that are not followed by a correct name in         **
  1985. **                  source_file[] and prints a warning in fout_ptr     **
  1986. **                  for each match found.                              **
  1987. **                                                                     **
  1988. ** REMARKS        : typedef is special : user_type_name looks first    **
  1989. **                  for a } and only after it for a correct name.      **
  1990. **                                                                     **
  1991. ** FUNCTIONS      : strncmp(), compile(), find_keyword(), fprintf()    **
  1992. **                                                                     **
  1993. ** GLOBALS        : source_file :                                      **
  1994. **                                                                     **
  1995. ** PARAMETERS     : nb_line :number of lines to examine in source_file **
  1996. **                  fout_ptr:points to output file                     **
  1997. **                                                                     **
  1998. ** PRECONDITIONS  : source_file[] must contain an ASCII file with all  **
  1999. **                  '\n' replaced by '\0'.                             **
  2000. **                                                                     **
  2001. ** POSTCONDITIONS : file pointed to by fout_ptr will contain results   **
  2002. **                  of checking process.                               **
  2003. **                                                                     **
  2004. **                                                                     **
  2005. ***C*F*E****************************************************************/
  2006.   CHAR   pbuf1[PMAX];                   /* compiled type name          */
  2007.   CHAR   pbuf2[PMAX];                   /* compiled begin of var name  */  
  2008.   CHAR   pbuf3[PMAX];                   /* ...for typedef              */
  2009.   T_LOCA loca_1;                        /* current loca of type name   */
  2010.   T_LOCA loca_2;                        /* current loca of correct name*/
  2011.   LONG   i;                             /* universal counter           */
  2012.   CHAR   *st_ptr;                       /* start of the search         */
  2013.   LONG   ligne;                         /* start of the search         */
  2014.   LONG   end_line;                      /* end of the search           */
  2015.  
  2016. /*---------- Initialize variables ---------------------------------------------*/
  2017.   
  2018.   CHAR *pattern_ptr[] =                                 /* patterns to compile */
  2019.   {                                                    /* in pbuf1 (type name) */
  2020.           "^ *struct "    ,"S_",       /* in pbuf2 (start of correct var name) */
  2021.           "^ *enum "      ,"E_",   
  2022.           "^ *union "     ,"U_",
  2023.           "^ *const "     ,"C_",
  2024.           "^ *typedef "   ,"}" , "T_",
  2025.           "the_end" 
  2026.   };
  2027.   ligne     = 1;
  2028.   st_ptr    = source_file;                                       /* first line */
  2029.   end_line  = nb_line;
  2030.  
  2031. /*---------- Look for each non correct user type or constant name -------------*/
  2032.  
  2033.   for ( i=0; strncmp(pattern_ptr[i],"the_end",7) NEQ 0 ; i = i+2)
  2034.   {
  2035.      /*---- compile patterns of type name and start of correct var name -------*/
  2036.  
  2037.      compile(pattern_ptr[i],  pbuf1);
  2038.      compile(pattern_ptr[i+1],pbuf2);
  2039.  
  2040.      loca_1 = find_keyword(st_ptr, ligne, nb_line, pbuf1); /* loc of type name */
  2041.  
  2042.      if (i EQ 8)                                       /* typedef : 3 patterns */
  2043.      {                                                 /* to compile           */
  2044.         compile(pattern_ptr[i+2],pbuf3);
  2045.         i++;
  2046.      }
  2047.  
  2048.      /*------ Look for each declaration of the same type ----------------------*/
  2049.      
  2050.      while ((loca_1.line_no)> 0)                      /* while type name found */
  2051.      {
  2052.        if ((loca_1.line_no < nb_line -1 ) AND (i NEQ 9))          /* for speed */
  2053.        {
  2054.           end_line = loca_1.line_no + 1;
  2055.        }
  2056.  
  2057.        /*---- try to find a name beginning as expected ------------------------*/
  2058.  
  2059.        loca_2 = find_keyword(loca_1.end_ptr, loca_1.line_no, end_line, pbuf2);
  2060.  
  2061.        if ( i EQ 9 )                                 /* typedef : look for }T_ */
  2062.        {
  2063.           loca_1 = loca_2;
  2064.           loca_2 = find_keyword(loca_1.end_ptr,loca_1.line_no, end_line, pbuf3);
  2065.        }
  2066.  
  2067.        /*---- Warning if no correct name found in the declaration -------------*/
  2068.  
  2069.        if ( loca_1.line_no NEQ loca_2.line_no ) 
  2070.        {
  2071.          fprintf(fout_ptr,"\nWARNING (line %4ld) : the name of this identifier or constant is wrong,", loca_1.line_no);
  2072.          fprintf(fout_ptr," it should begin with %s .\n",pattern_ptr[i+1]);
  2073.        }
  2074.        st_ptr    = loca_1.end_ptr;
  2075.        ligne     = loca_1.line_no;
  2076.        end_line  = nb_line;
  2077.        loca_1    = find_keyword(st_ptr, ligne, nb_line, pbuf1); /* next match */
  2078.      }
  2079.      st_ptr = source_file;
  2080.      ligne  = 1;                     /* go back to beginning of source_file[] */
  2081.   } 
  2082. }
  2083.  
  2084. void ptr_name(CHAR source_file[], LONG nb_line, FILE *fout_ptr) 
  2085. /**C*F*******************************************************************
  2086. **                                                                     **
  2087. ** SRC-MODULE     : ptr_name                                           **
  2088. ** VERSION        : 001                                                **
  2089. ** LONG_NAME      : pointer name                                       **
  2090. **                                                                     **
  2091. **                                                                     **
  2092. ** SPECIFICATION  : C coding conventions                               **
  2093. ** SUBSYSTEM      : -                                                  **
  2094. ** SYSTEM_TYPE    : PCD-3T                                             **
  2095. **                                                                     **
  2096. ** AUTHOR         : Helene Ballay                                      **
  2097. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  2098. **                                                                     **
  2099. ** DESCRIPTION    : ptr_name() looks in source_file[] for declarations **
  2100. **                  of pointers and then looks for a declaration of    **
  2101. **                  correct name (ending with _ptr). If none is found  **
  2102. **                  in the same line, it prints a warning in fout_ptr. **
  2103. **                                                                     **
  2104. ** REMARKS        : names of pointers to a type that has been defined  **
  2105. **                  in the define part are not checked.                **
  2106. **                                                                     **
  2107. ** FUNCTIONS      : strncmp(), compile(), find_keyword(), fprintf()    **
  2108. **                                                                     **
  2109. ** GLOBALS        : source_file                                        **
  2110. **                                                                     **
  2111. ** PARAMETERS     : nb_line :number of lines to examine in source_file **
  2112. **                  fout_ptr:points to output file                     **
  2113. **                                                                     **
  2114. ** PRECONDITIONS  : source_file[] must contain an ASCII file with all  **
  2115. **                  '\n' replaced by '\0'.                             **
  2116. **                                                                     **
  2117. ** POSTCONDITIONS : file pointed to by fout_ptr will contain results   **
  2118. **                  of checking process.                               **
  2119. **                                                                     **
  2120. **                                                                     **
  2121. ***C*F*E****************************************************************/
  2122.   CHAR   pbuf1[PMAX];                   /* compiled patt: pointer      */
  2123.   CHAR   pbuf2[PMAX];                   /* compiled patt: pointer_ptr  */
  2124.   T_LOCA loca_1;                        /* location of a pointer       */
  2125.   T_LOCA loca_2;                        /* location of a pointer_ptr   */
  2126.   LONG   i;                             /* universal counter           */
  2127.   CHAR   *st_ptr;                       /* start of the search         */
  2128.   LONG   ligne;                         /* start of the search         */
  2129.   LONG   end_line;                      /* end of the search           */
  2130.  
  2131. /*---------- Initialize variables ---------------------------------------------*/
  2132.   
  2133.   CHAR *false_ptr[] =                /* patterns to compile in pbuf1 and pbuf2 */
  2134.   { 
  2135.           "FILE  *\\*:v+ *[;,)]"   ,"FILE  *\\*:v+_ptr *[;,)]",
  2136.           "CHAR  *\\*:v+ *[;,)]"   ,"CHAR  *\\*:v+_ptr *[;,)]", /* matches CHAR and UCHAR */
  2137.           "LONG  *\\*:v+ *[;,)]"   ,"LONG  *\\*:v+_ptr *[;,)]", /* matches LONG and ULONG */
  2138.           "SHORT +\\*:v+ *[;,)]"   ,"SHORT *\\*:v+_ptr *[;,)]", /* matches SHORT and USHORT */
  2139.           "BITFIELD +\\*:v+ *[;,)]","BITFIELD +\\*:v+_ptr *[;,)]",
  2140.           "S_[A-Z0-9_]+ +\\*:v+ *[;,)]"   ,"S_[A-Z0-9_]+ +\\*:v+_ptr *[;,)]",
  2141.           "T_[A-Z0-9_]+ +\\*:v+ *[;,)]"   ,"T_[A-Z0-9_]+ +\\*:v+_ptr *[;,)]",
  2142.           "U_[A-Z0-9_]+ +\\*:v+ *[;,)]"   ,"U_[A-Z0-9_]+ +\\*:v+_ptr *[;,)]",
  2143.           "E_[A-Z0-9_]+ +\\*:v+ *[;,)]"   ,"E_[A-Z0-9_]+ +\\*:v+_ptr *[;,)]",
  2144.           " (\\*:v+) *( *) *[;,)]" ," (\\*:v+_ptr) *( *) *[;,)]",     /* ptr to fction */
  2145.           "FILE  *\\*\\*:v+ *[;,)]"   ,"FILE  *\\*\\*:v+_pptr *[;,)]",
  2146.           "CHAR  *\\*\\*:v+ *[;,)]"   ,"CHAR  *\\*\\*:v+_pptr *[;,)]", /* matches CHAR and UCHAR */
  2147.           "LONG  *\\*\\*:v+ *[;,)]"   ,"LONG  *\\*\\*:v+_pptr *[;,)]", /* matches LONG and ULONG */
  2148.           "SHORT +\\*\\*:v+ *[;,)]"   ,"SHORT *\\*\\*:v+_pptr *[;,)]", /* matches SHORT and USHORT */
  2149.           "BITFIELD +\\*\\*:v+ *[;,)]","BITFIELD +\\*\\*:v+_pptr *[;,)]",
  2150.           "S_[A-Z0-9_]+ +\\*\\*:v+ *[;,)]"   ,"S_[A-Z0-9_]+ +\\*\\*:v+_pptr *[;,)]",
  2151.           "T_[A-Z0-9_]+ +\\*\\*:v+ *[;,)]"   ,"T_[A-Z0-9_]+ +\\*\\*:v+_pptr *[;,)]",
  2152.           "U_[A-Z0-9_]+ +\\*\\*:v+ *[;,)]"   ,"U_[A-Z0-9_]+ +\\*\\*:v+_pptr *[;,)]",
  2153.           "E_[A-Z0-9_]+ +\\*\\*:v+ *[;,)]"   ,"E_[A-Z0-9_]+ +\\*\\*:v+_pptr *[;,)]",
  2154.           " (\\*\\*:v+) *( *) *[;,)]" ," (\\*\\*:v+_pptr) *( *) *[;,)]",     /* ptr to fction */
  2155.  
  2156.           "the_end" 
  2157.   };
  2158.   ligne   = 1;
  2159.   st_ptr   = source_file;                                        /* first line */
  2160.  
  2161. /*---------- Look for each non correct expression -----------------------------*/
  2162.  
  2163.   for ( i=0; strncmp(false_ptr[i],"the_end",7) NEQ 0 ; i = i+2)
  2164.   {
  2165.      /*----- compile each pattern coresponding to a pointer declaration -------*/
  2166.      /*----- and look for a match ---------------------------------------------*/
  2167.  
  2168.      compile(false_ptr[i],pbuf1);
  2169.      loca_1 = find_keyword(st_ptr, ligne, nb_line, pbuf1);
  2170.  
  2171.      if (loca_1.line_no < (nb_line -1))                    /* useful for speed */
  2172.          end_line = loca_1.line_no +1;
  2173.      else
  2174.          end_line = nb_line;
  2175.  
  2176.      /*---- compile each pattern of a declaration of pointer ending with _ptr --*/
  2177.      /*---- and look for a match -----------------------------------------------*/
  2178.  
  2179.      compile(false_ptr[i+1],pbuf2);
  2180.      loca_2 = find_keyword(st_ptr, ligne, end_line, pbuf2);
  2181.      
  2182.      while ((loca_1.line_no)> 0) /* while non examined ptr declaration are left */
  2183.      {
  2184.        if (loca_2.end_ptr NEQ loca_1.end_ptr)
  2185.        {
  2186.          /* fprintf(fout_ptr,"\nMATCH AT PATTERN NO. %4ld",i); */    /* Debug code */
  2187.          fprintf(fout_ptr,"\nWARNING (line %4ld) : the name of this pointer is wrong,", loca_1.line_no);
  2188.          if (i < 20)
  2189.            {
  2190.              fprintf(fout_ptr," it should end with _ptr.");
  2191.            }
  2192.          else
  2193.            {
  2194.              fprintf(fout_ptr," it should end with _pptr. As an alternative");
  2195.              fprintf(fout_ptr," you could use index notation which most often enhances readability.\n");
  2196.            }
  2197.        }
  2198.        st_ptr  = loca_1.end_ptr;
  2199.        ligne   = loca_1.line_no;
  2200.  
  2201.        /*------ next declaration of pointer ------------------------------------*/
  2202.  
  2203.        loca_1     = find_keyword(st_ptr, ligne, nb_line, pbuf1);
  2204.  
  2205.        if (loca_1.line_no < (nb_line -1))                   /* useful for speed */
  2206.          end_line = loca_1.line_no +1;
  2207.        else
  2208.          end_line = nb_line;
  2209.  
  2210.        /*------ next declaration of pointer with name ending with _ptr ---------*/
  2211.  
  2212.        loca_2     = find_keyword(st_ptr, ligne, end_line, pbuf2); 
  2213.      }
  2214.      st_ptr = source_file;
  2215.      ligne     = 1;                    /* go back to beginning of source_file[] */
  2216.   }
  2217. }
  2218.  
  2219. void switch_stat(CHAR source_file[], LONG nb_line, FILE *fout_ptr) 
  2220. /**C*F*******************************************************************
  2221. **                                                                     **
  2222. ** SRC-MODULE     : switch_stat                                        **
  2223. ** VERSION        : 001                                                **
  2224. ** LONG_NAME      : switch statement                                   **
  2225. **                                                                     **
  2226. **                                                                     **
  2227. ** SPECIFICATION  : C coding conventions                               **
  2228. ** SUBSYSTEM      : -                                                  **
  2229. ** SYSTEM_TYPE    : PCD-3T                                             **
  2230. **                                                                     **
  2231. ** AUTHOR         : Helene Ballay                                      **
  2232. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  2233. **                                                                     **
  2234. ** DESCRIPTION    : switch_stat looks for each switch statement in     **
  2235. **                  source_file[] and looks for code between switch    **
  2236. **                  and case or break and case. It prints a warning    **
  2237. **                  in fout_ptr if there is code between switch and    **
  2238. **                  first case or if there is no break before case.    **
  2239. **                                                                     **
  2240. ** REMARKS        : the last break is not checked                      **
  2241. **                                                                     **
  2242. ** FUNCTIONS      : compile(), find_keyword(), fprintf()               **
  2243. **                                                                     **
  2244. ** GLOBALS        : source_file                                        **
  2245. **                                                                     **
  2246. ** PARAMETERS     : nb_line :number of lines to examine in source_file **
  2247. **                  fout_ptr:points to output file                     **
  2248. **                                                                     **
  2249. ** PRECONDITIONS  : source_file[] must contain an ASCII file with all  **
  2250. **                  '\n' replaced by '\0'.                             **
  2251. **                                                                     **
  2252. ** POSTCONDITIONS : file pointed to by fout_ptr will contain results   **
  2253. **                  of checking process.                               **
  2254. **                                                                     **
  2255. **                                                                     **
  2256. ***C*F*E****************************************************************/
  2257.                                         /* compiled patterns :         */
  2258.   CHAR   pswitch[PMAX];                                      /* switch */                       
  2259.   CHAR   pcase[PMAX];                                        /* case   */
  2260.   CHAR   pbreak[PMAX];                                       /* break  */
  2261.   CHAR   psth[PMAX];                       /* anything but space and } */
  2262.   T_LOCA l_switch;                      /* current location of switch  */
  2263.   T_LOCA l_break;                       /* current location of break   */
  2264.   T_LOCA l_case1;                       /* location of first case      */
  2265.   T_LOCA l_case2;                       /* location of second case     */
  2266.   T_LOCA l_between;                     /* current location of any     */
  2267.                                         /* unexpected character        */
  2268.   CHAR   *start_ptr;                    /* start of the search         */
  2269.   LONG   ligne;                         /* start of the search         */
  2270.  
  2271. /*---------- Initialize variables ---------------------------------------------*/
  2272.  
  2273.   ligne         = 1;
  2274.   start_ptr     = source_file;                                   /* first line */
  2275.  
  2276.   compile(" switch *(.*)",pswitch);
  2277.   compile(" case ",pcase);
  2278.   compile(" break;",pbreak);
  2279.   compile("[¡-ÿ!-z|-~]",psth);             /* ASCII : anything but { and space */
  2280.  
  2281. /*---------- Look for each non correct expression -----------------------------*/
  2282.  
  2283.   l_switch = find_keyword(start_ptr, ligne, nb_line, pswitch);
  2284.  
  2285.   while ( l_switch.line_no > 1)     /* while there is a switch stat to examine */
  2286.   {
  2287.     start_ptr = l_switch.end_ptr;
  2288.     ligne     = l_switch.line_no;
  2289.     l_switch  = find_keyword(start_ptr, ligne, nb_line, pswitch);
  2290.  
  2291.     /*-------- check presence of code between switch and first case -----------*/
  2292.  
  2293.     l_case1   = find_keyword(start_ptr, ligne, nb_line, pcase);
  2294.     l_between = find_keyword(start_ptr, ligne, nb_line, psth);
  2295.  
  2296.     if (l_between.line_no < l_case1.line_no)
  2297.     {
  2298.        fprintf(fout_ptr,"\nWARNING (line %4ld) : there should be no ",l_between.line_no);
  2299.        fprintf(fout_ptr,"program code between the switch statement and the first case label.");
  2300.     }
  2301.  
  2302.     /*-------- look for second case-block -------------------------------------*/
  2303.     /*-------- (cases in two following lines belong to the same block) --------*/
  2304.  
  2305.     l_case2   = find_keyword(l_case1.end_ptr, l_case1.line_no, nb_line, pcase);
  2306.  
  2307.     while ((l_case2.line_no NEQ 0) AND ((l_case2.line_no - l_case1.line_no) <=1))
  2308.     {
  2309.       l_case1  = l_case2;
  2310.       l_case2  = find_keyword(l_case1.end_ptr, l_case1.line_no, nb_line, pcase);
  2311.     }
  2312.     if (l_case2.line_no EQ 0)                                        /* if none */
  2313.        break;                     /* go back to while loop (examine next switch)*/
  2314.  
  2315.     /*-------- examine each case-block before next switch ----------------------*/
  2316.  
  2317.     while ((l_switch.line_no EQ 0 ) OR (l_switch.line_no > l_case2.line_no))
  2318.     {
  2319.       l_break = find_keyword(start_ptr, ligne, nb_line, pbreak);
  2320.    
  2321.       /*------ look for a break just before the case(2) statement --------------*/
  2322.     
  2323.       while ((l_break.line_no NEQ 0) AND (l_break.line_no < l_case2.line_no))
  2324.       {
  2325.          l_between = find_keyword(l_break.end_ptr, l_break.line_no, nb_line, psth);
  2326.          if (l_between.line_no >= l_case2.line_no )
  2327.              break;
  2328.          l_break   = find_keyword(l_break.end_ptr, l_break.line_no, nb_line, pbreak);
  2329.       }
  2330.  
  2331.       /*------ if break is missing ---------------------------------------------*/
  2332.  
  2333.       if ((l_break.line_no EQ 0) OR (l_break.line_no > l_case2.line_no))
  2334.       {
  2335.          fprintf(fout_ptr,"\nWARNING (line %4ld) : there is no break before this case. ",l_case2.line_no);
  2336.          fprintf(fout_ptr,"If the fall-through behaviour is deliberate, don't forget extensive comments !");         
  2337.       } 
  2338.  
  2339.       /*------ next case statement ---------------------------------------------*/
  2340.  
  2341.       do
  2342.       {
  2343.         l_case1 = l_case2;
  2344.         l_case2 = find_keyword(l_case1.end_ptr,l_case1.line_no,nb_line,pcase);
  2345.       }while ((l_case2.line_no NEQ 0) AND ((l_case2.line_no - l_case1.line_no) <= 1));
  2346.  
  2347.       if (l_case2.line_no EQ 0)           /* (if no case left go back to first) */
  2348.           break;                         /* (while loop ie examine next switch) */ 
  2349.     }
  2350.   }
  2351. }
  2352. void default_stat(CHAR source_file[], LONG nb_line, FILE *fout_ptr)
  2353. /**C*F*******************************************************************
  2354. **                                                                     **
  2355. ** SRC-MODULE     : default_stat                                       **
  2356. ** VERSION        : 001                                                **
  2357. ** LONG_NAME      : default statement                                  **
  2358. **                                                                     **
  2359. **                                                                     **
  2360. ** SPECIFICATION  : C coding conventions                               **
  2361. ** SUBSYSTEM      : -                                                  **
  2362. ** SYSTEM_TYPE    : PCD-3T                                             **
  2363. **                                                                     **
  2364. ** AUTHOR         : Helene Ballay                                      **
  2365. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  2366. **                                                                     **
  2367. ** DESCRIPTION    : default_stat() looks for a switch selection and    **
  2368. **                  calls recurs() for each one found, which looks     **
  2369. **                  for nested switch selection and works recursively  **
  2370. **                  to find a default label in each selection. If none **
  2371. **                  is found, a warning is printed in output file.     **
  2372. **                                                                     **
  2373. ** REMARKS        :                                                    **
  2374. **                                                                     **
  2375. ** FUNCTIONS      : compile(), find_keyword(), fprintf(), recurs(),    **
  2376. **                  return()                                           ** 
  2377. **                                                                     **
  2378. ** GLOBALS        : source_file[]                                      **
  2379. **                                                                     **
  2380. ** PARAMETERS     : nb_line :number of lines to examine in source_file **
  2381. **                  fout_ptr:points to output file                     **
  2382. **                                                                     **
  2383. ** PRECONDITIONS  : source_file[] must contain an ASCII file with all  **
  2384. **                  '\n' replaced by '\0'.                             **
  2385. **                                                                     **
  2386. ** POSTCONDITIONS : file pointed to by fout_ptr will contain results   **
  2387. **                  of checking process.                               **
  2388. **                                                                     **
  2389. **                                                                     **
  2390. ***C*F*E****************************************************************/
  2391. {
  2392.   T_LOCA l_end_tst;              /* end of code checked by recurs()    */
  2393.   T_LOCA l_switch;               /* position of current switch         */
  2394.   CHAR   psw[PMAX];              /* pattern template : switch          */ 
  2395.   CHAR   pdef[PMAX];             /* pattern template : default         */
  2396.   CHAR   *start_ptr;             /* start of search for switch         */
  2397.   LONG   ligne;                  /* start of search for switch         */
  2398.  
  2399.   /*------- Initialize variables ----------------------------------------------*/
  2400.  
  2401.   start_ptr = source_file;
  2402.   ligne     = 1;
  2403.   compile(" switch *(",psw);
  2404.   compile(" default[ :]",pdef);  
  2405.   l_switch  = find_keyword(start_ptr,ligne,nb_line,psw);
  2406.  
  2407.  
  2408.   /*------- Scan source_file[] for switch selection, calling recursive() ------*/
  2409.   /*------- using recursion for nested switch selections ----------------------*/
  2410.  
  2411.   while (l_switch.line_no NEQ 0)                  /* while non examined switch */ 
  2412.   {
  2413.      l_end_tst = recurs(l_switch,nb_line,psw,pdef,fout_ptr);
  2414.      start_ptr = l_end_tst.end_ptr;
  2415.      ligne     = l_end_tst.line_no;
  2416.      l_switch  = find_keyword(start_ptr,ligne,nb_line,psw);      /* next match */
  2417.   }
  2418. }
  2419. /*-----------------------------------------------------------------------------*/
  2420. /*--------- Recursive checking function ---------------------------------------*/
  2421. /*-----------------------------------------------------------------------------*/
  2422.  
  2423. T_LOCA recurs(T_LOCA l_switch, LONG nb_line, CHAR psw[], CHAR pdef[], FILE *fout_ptr) 
  2424. {
  2425.   T_LOCA l_nxt_sw;                   /* location of next switch         */
  2426.   T_LOCA l_default;                  /* location of current default     */
  2427.   T_LOCA l_open;                     /* location of current {           */
  2428.   T_LOCA l_close;                    /* location of current }           */
  2429.   T_LOCA l_end_sw;                   /* end of current switch selection */
  2430.   T_LOCA l_end_tst;                  /* end of last examined switch     */
  2431.                                      /* selection :returned by recurs() */
  2432.   CHAR   *start_ptr;                 /* start of the search             */
  2433.   LONG   ligne;                      /* start of the search             */
  2434.   LONG   count;                      /* counts braces                   */ 
  2435.   CHAR   popen[PMAX];                /* pattern template : opened brace */
  2436.   CHAR   pclose[PMAX];               /* pattern template : closed brace */
  2437.  
  2438.   /*------ Initialize variables ----------------------------------------------*/
  2439.  
  2440.   start_ptr = l_switch.end_ptr;          /* start search after current switch */
  2441.   ligne     = l_switch.line_no;
  2442.   l_nxt_sw  = find_keyword(start_ptr,ligne,nb_line,psw);       /* next switch */
  2443.   l_default = find_keyword(start_ptr,ligne,nb_line,pdef);          /* default */
  2444.   count     = 0;                                             /* reset counter */
  2445.  
  2446.   compile("{[^']",popen);                                  /* no quoted brace */
  2447.   compile("}[^']",pclose);                                 /* no quoted brace */
  2448.  
  2449.   /*----- find end of current switch statement -------------------------------*/
  2450.  
  2451.   l_open  = find_keyword(start_ptr,ligne,nb_line,popen);           /* first { */
  2452.   l_close = find_keyword(start_ptr,ligne,nb_line,pclose);          /* first } */
  2453.  
  2454.   do
  2455.   {
  2456.      while (( l_open.line_no < l_close.line_no ) AND (l_open.line_no NEQ 0))
  2457.      {
  2458.        count++;
  2459.        start_ptr = l_open.end_ptr;
  2460.        ligne     = l_open.line_no;
  2461.        l_open    = find_keyword(start_ptr,ligne,nb_line,popen);
  2462.      } 
  2463.      while (( l_close.line_no < l_open.line_no ) OR (l_open.line_no EQ 0))
  2464.      {
  2465.        count--;
  2466.        if (count EQ 0)                  /* if end of switch selection found */
  2467.        {
  2468.           l_end_sw = l_close;
  2469.           break;                     
  2470.        }
  2471.        start_ptr = l_close.end_ptr;
  2472.        ligne     = l_close.line_no;
  2473.        l_close   = find_keyword(start_ptr,ligne,nb_line,pclose);
  2474.      }
  2475.   }while (count NEQ 0);
  2476.  
  2477.   /*----- if no switch and no default in current switch block ----------------*/
  2478.  
  2479.   if (     ((l_nxt_sw.line_no  EQ 0) OR (l_nxt_sw.line_no > l_end_sw.line_no ))
  2480.        AND ((l_default.line_no EQ 0) OR (l_end_sw.line_no < l_default.line_no)) )
  2481.   {
  2482.      fprintf(fout_ptr,"\nWARNING (line %4ld) : every switch selection must ", l_switch.line_no);
  2483.      fprintf(fout_ptr,"exhibit a default label.");
  2484.      return(l_end_sw);
  2485.   }
  2486.   /*----- if other switch to examine in current switch block ----------------*/
  2487.  
  2488.   else if ((l_nxt_sw.line_no NEQ 0) AND ( l_nxt_sw.line_no < l_end_sw.line_no ))
  2489.   {
  2490.     /*--- examine all switches within the current switch block --------------*/
  2491.  
  2492.     do
  2493.     {
  2494.       l_end_tst = recurs(l_nxt_sw,nb_line,psw,pdef,fout_ptr); 
  2495.       start_ptr = l_end_tst.end_ptr;
  2496.       ligne     = l_end_tst.line_no;
  2497.  
  2498.       l_nxt_sw  = find_keyword(start_ptr,ligne,nb_line,psw);
  2499.       l_default = find_keyword(start_ptr,ligne,nb_line,pdef);
  2500.  
  2501.     }while((l_nxt_sw.line_no < l_end_sw.line_no) AND (l_nxt_sw.line_no NEQ 0));
  2502.  
  2503.     /*--- check presence of default for current switch block ----------------*/
  2504.  
  2505.     if ((l_default.line_no EQ 0) OR (l_default.line_no > l_end_sw.line_no))
  2506.     {
  2507.         fprintf(fout_ptr,"\nWARNING (line %4ld) : every switch selection must ",l_switch.line_no);
  2508.         fprintf(fout_ptr,"exhibit a default label. ");
  2509.     }
  2510.   }
  2511.   /*---- examine next non nested switch selection ---------------------------*/
  2512.  
  2513.   return(l_end_sw);                /* return end of current switch selection */
  2514. }
  2515. void tst_loop(CHAR source_file[], LONG nb_line, FILE *fout_ptr)
  2516. /**C*F*******************************************************************
  2517. **                                                                     **
  2518. ** SRC-MODULE     : tst_loop                                           **
  2519. ** VERSION        : 001                                                **
  2520. ** LONG_NAME      : test of loop                                       **
  2521. **                                                                     **
  2522. **                                                                     **
  2523. ** SPECIFICATION  : C coding conventions                               **
  2524. ** SUBSYSTEM      : -                                                  **
  2525. ** SYSTEM_TYPE    : PCD-3T                                             **
  2526. **                                                                     **
  2527. ** AUTHOR         : Helene Ballay                                      **
  2528. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  2529. **                                                                     **
  2530. ** DESCRIPTION    : tst_loop() looks for implicit tests and assignments**
  2531. **                  in conditional statements and looks if a pointer   **
  2532. **                  value is compared with 0 instead of NULL after     **
  2533. **                  each loop in source_file[] and prints a            **
  2534. **                  warning in fout_ptr if necessary.                  **
  2535. **                                                                     **
  2536. ** REMARKS        :                                                    **
  2537. **                                                                     **
  2538. ** FUNCTIONS      : strncmp(), compile(), find_keyword(), fprintf()    **
  2539. **                                                                     **
  2540. ** GLOBALS        : source_file                                        **
  2541. **                                                                     **
  2542. ** PARAMETERS     : nb_line :number of lines to examine in source_file **
  2543. **                  fout_ptr:points to output file                     **
  2544. **                                                                     **
  2545. ** PRECONDITIONS  : source_file[] must contain an ASCII file with all  **
  2546. **                  '\n' replaced by '\0'.                             **
  2547. **                                                                     **
  2548. ** POSTCONDITIONS : file pointed to by fout_ptr will contain results   **
  2549. **                  of checking process.                               **
  2550. **                                                                     **
  2551. **                                                                     **
  2552. ***C*F*E****************************************************************/
  2553.   CHAR   pbuf_loop[PMAX];               /* loop pattern                */
  2554.   CHAR   pbuf_conj[PMAX];               /* conjonction pattern         */
  2555.   CHAR   pbuf_test[PMAX];               /* test pattern                */
  2556.   CHAR   popen[PMAX];                   /* patt (                      */
  2557.   CHAR   pclose[PMAX];                  /* patt )                      */
  2558.   T_LOCA loca_loop;                     /* current location of loop    */
  2559.   T_LOCA loca_conj;                     /* current location of conjonc */
  2560.   T_LOCA loca_tst;                      /* current location of test    */
  2561.   T_LOCA l_open;                        /* location of opened brace    */
  2562.   T_LOCA l_close;                       /* location of closed brace    */
  2563.   T_LOCA l_end_tst;                     /* end of ctrl flow statement  */
  2564.   LONG   i;                             /* universal counter           */
  2565.   LONG   j;                             /* universal counter           */
  2566.   CHAR   *start_ptr;                    /* place to start the search   */
  2567.   LONG   ligne;                         /* line to start the search    */
  2568.   LONG   end_line;                      /* line to end the search      */
  2569.   LONG   nb_tst;                        /* nb of tests in control stat */
  2570.   LONG   nb_expl_tst;                   /* nb of explicit tests in the */
  2571.                                         /* current control statement   */
  2572.   LONG   count;                         /* counts braces to find end of*/
  2573.                                         /* control flow statement      */
  2574.   LONG   bip;                           /* flag used to find the exact */
  2575.                                         /* end of brace                */
  2576.  
  2577. /*---------- Initialize variables ---------------------------------------------*/
  2578.   
  2579.   CHAR *loop_ptr[] =               /* patterns to compile : loops if and while */
  2580.   {
  2581.           "[^d]if[ (]",                   /* neither endif nor ifndef or ifdef */
  2582.           "while[ (]",
  2583.           "the_end"
  2584.   };         
  2585.   CHAR *pattern_conj[] =                 /* patterns to compile : conjonctions */
  2586.   {                              /* (there is one more test than conjonctions) */
  2587.        "[ )]AND[ (]","[ )]AND$",
  2588.        "[ )]OR[ (]","[) ]OR$",
  2589.        "&&[ (]","&&$",              /* conjonctions are followed by space or ( */
  2590.        "||[ (]","||$",                      /* or at the end of a line */
  2591.        "the_end"
  2592.   };
  2593.   CHAR *pattern_test[] =               /* patterns to compile : explicit tests */
  2594.   {
  2595.        " N*EQ",                                                   /* EQ or NEQ */
  2596.        "[!=]=",                                                    /* == or != */
  2597.        "[^\\-][<>]=*",                                          /* comparaison */
  2598.        "the_end"
  2599.   };
  2600.   ligne       = 1;
  2601.   start_ptr   = source_file;                                     /* first line */
  2602.   nb_tst      = 1;
  2603.   nb_expl_tst = 0; 
  2604.   count       = 0;                                 /* set all counters to zero */
  2605.   bip         = 0;
  2606.   compile("([^']",popen);                                  /* non quoted brace */
  2607.   compile(")[^']",pclose);                                 /* non quoted brace */
  2608.  
  2609. /*---------- Look for each loop with a bad test method ------------------------*/
  2610.  
  2611.   for ( i=0; strncmp(loop_ptr[i],"the_end",7) NEQ 0 ; i++)
  2612.   {
  2613.      compile(loop_ptr[i],pbuf_loop);
  2614.      loca_loop = find_keyword(start_ptr, ligne, nb_line, pbuf_loop);
  2615.      
  2616.      while ((loca_loop.line_no)> 0)                 /* while loop not examined */
  2617.      {
  2618.        start_ptr = loca_loop.end_ptr;
  2619.        if ( *(start_ptr-1) EQ '(')      /* if brace included in the loop match */
  2620.           start_ptr--;                      /* go back to examine it too */
  2621.        ligne     = loca_loop.line_no;
  2622.  
  2623.        /*------ look for end of tests of the loop ----------------------------*/
  2624.  
  2625.        l_open  = find_keyword(start_ptr,ligne,nb_line,popen);      /* first ( */
  2626.        l_close = find_keyword(start_ptr,ligne,nb_line,pclose);     /* first ) */
  2627.  
  2628.        if ( (*l_close.end_ptr NEQ '\0') OR
  2629.                   ((*l_close.end_ptr EQ '\0') AND (*(l_close.end_ptr-2) EQ ')') AND (l_close.end_ptr NEQ (start_ptr+1))) )
  2630.        {
  2631.           bip=0;           /* bip is a flag used to know the exact end of the */
  2632.        }                   /* brace examined : l_close points  after the char */
  2633.        else                /* following the brace (bip=0) to check it is no   */
  2634.        {                   /* quote but if the brace is at the end of the line*/
  2635.           bip=1;           /* it points to \0 (bip=1),ie just after the brace */
  2636.        }
  2637.        do     /* go through braces until end of control flow statement found */
  2638.        {
  2639.           while (( l_open.end_ptr < l_close.end_ptr ) AND (l_open.line_no NEQ 0))
  2640.           {
  2641.              count++;
  2642.              start_ptr = l_open.end_ptr;
  2643.              if (*start_ptr NEQ '\0')
  2644.                 start_ptr--;
  2645.              ligne     = l_open.line_no;
  2646.              l_open    = find_keyword(start_ptr,ligne,nb_line,popen); /* next */
  2647.           }
  2648.           while (( l_close.end_ptr < l_open.end_ptr ) OR (l_open.line_no EQ 0))
  2649.           {
  2650.              count--;
  2651.              if (count EQ 0)                       /* if end of test(s) found */
  2652.              {
  2653.                  l_end_tst = l_close;
  2654.                  break;                     
  2655.              }
  2656.              if (bip EQ 1)                           /* start_ptr points just */
  2657.                 start_ptr = l_close.end_ptr;            /* after the brace */
  2658.              else                                           /* examined */
  2659.                 start_ptr = l_close.end_ptr - 1;
  2660.  
  2661.              ligne     = l_close.line_no;
  2662.              l_close   = find_keyword(start_ptr,ligne,nb_line,pclose);
  2663.  
  2664.              if ( (*l_close.end_ptr NEQ '\0') OR
  2665.                   ((*l_close.end_ptr EQ '\0') AND (*(l_close.end_ptr-2) EQ ')') AND (l_close.end_ptr NEQ (start_ptr+1))) )
  2666.              {
  2667.                 bip=0;                             /* match was two char long */
  2668.              }
  2669.              else
  2670.              { 
  2671.                 bip=1;                             /* match was one char long */  
  2672.              }
  2673.           }
  2674.        }while (count NEQ 0);
  2675.  
  2676.        if (l_end_tst.line_no < nb_line)                  /* useful for speed  */
  2677.            end_line = l_end_tst.line_no +1;
  2678.        else
  2679.            end_line = nb_line;
  2680.      
  2681.        /*------ look for conjunctions between tests of the loop --------------*/
  2682.  
  2683.        for ( j=0 ; strncmp(pattern_conj[j],"the_end",7) NEQ 0 ; j++)
  2684.        {
  2685.            compile(pattern_conj[j],pbuf_conj);
  2686.            start_ptr = loca_loop.end_ptr;
  2687.            ligne     = loca_loop.line_no;
  2688.            loca_conj = find_keyword(start_ptr, ligne, end_line, pbuf_conj);
  2689.  
  2690.            while ((loca_conj.line_no NEQ 0) AND (loca_conj.end_ptr < l_end_tst.end_ptr)) 
  2691.            {
  2692.               nb_tst++;
  2693.               start_ptr = loca_conj.end_ptr;
  2694.               ligne     = loca_conj.line_no;
  2695.               loca_conj = find_keyword(start_ptr, ligne, end_line, pbuf_conj);
  2696.            }
  2697.        }
  2698.        
  2699.        /*------ look for explicit tests --------------------------------------*/
  2700.  
  2701.        for ( j=0 ; strncmp(pattern_test[j],"the_end",7) NEQ 0 ; j++)
  2702.        {
  2703.            compile(pattern_test[j],pbuf_test);
  2704.            start_ptr = loca_loop.end_ptr;
  2705.            ligne     = loca_loop.line_no;      
  2706.            loca_tst  = find_keyword(start_ptr, ligne, end_line, pbuf_test);
  2707.  
  2708.            while ((loca_tst.line_no NEQ 0) AND (loca_tst.end_ptr < l_end_tst.end_ptr)) 
  2709.            {
  2710.               nb_expl_tst++;
  2711.               start_ptr = loca_tst.end_ptr;
  2712.               ligne     = loca_tst.line_no;
  2713.               loca_tst  = find_keyword(start_ptr, ligne, end_line, pbuf_test);
  2714.            }      
  2715.        }
  2716.  
  2717.        /*------ if not enough explicit tests ---------------------------------*/
  2718.  
  2719.        if ( nb_expl_tst NEQ nb_tst)
  2720.           fprintf( fout_ptr ,"\nWARNING (line %4ld) : do please use explicit test(s). ",loca_loop.line_no);
  2721.  
  2722.        /*----- look for assignement ------------------------------------------*/
  2723.  
  2724.        start_ptr = loca_loop.end_ptr;
  2725.        ligne     = loca_loop.line_no;
  2726.        compile("[^><=!]=[^=]",pbuf_test);
  2727.        loca_tst = find_keyword(start_ptr, ligne, end_line, pbuf_test);
  2728.  
  2729.        if ((loca_tst.end_ptr < l_end_tst.end_ptr) AND (loca_tst.end_ptr NEQ NULL))
  2730.           fprintf(fout_ptr,"\nWARNING (line %4ld) : assignement in control flow statement should be avoided.",loca_tst.line_no);
  2731.  
  2732.        /*----- look for use of zero as a pointer value ----------------------*/
  2733.  
  2734.        compile("[^*][a-z0-9_]+_ptr *N*EQ +0",pbuf_test);        
  2735.        loca_tst = find_keyword(start_ptr, ligne, end_line, pbuf_test);
  2736.  
  2737.        if ((loca_tst.end_ptr < l_end_tst.end_ptr) AND (loca_tst.end_ptr NEQ NULL))
  2738.           fprintf(fout_ptr,"\nWARNING (line %4ld) : pointer value 0 shall be implemented via the pointer constant NULL.",loca_tst.line_no);
  2739.  
  2740.        /*----- next loop of the same type -----------------------------------*/
  2741.  
  2742.        loca_loop   = find_keyword(start_ptr, ligne, nb_line, pbuf_loop);
  2743.        nb_tst      = 1;
  2744.        nb_expl_tst = 0; 
  2745.      }
  2746.      start_ptr = source_file;
  2747.      ligne     = 1;                 /* go back to beginning of source_file[] */
  2748.   } 
  2749. }
  2750. void lib_fct_call(CHAR source_file[], LONG nb_line, FILE *fout_ptr)
  2751. /**C*F*******************************************************************
  2752. **                                                                     **
  2753. ** SRC-MODULE     : lib_fct_call                                       **
  2754. ** VERSION        : 001                                                **
  2755. ** LONG_NAME      : library function call                              **
  2756. **                                                                     **
  2757. **                                                                     **
  2758. ** SPECIFICATION  : C coding conventions                               **
  2759. ** SUBSYSTEM      : -                                                  **
  2760. ** SYSTEM_TYPE    : PCD-3T                                             **
  2761. **                                                                     **
  2762. ** AUTHOR         : Helene Ballay                                      **
  2763. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  2764. **                                                                     **
  2765. ** DESCRIPTION    : lib_fct_call looks for each call of a library      **
  2766. **                  function that is not immediately followed by       **
  2767. **                  an if statement and prints a warning in fout_ptr   **
  2768. **                  if a match is found in source_file[].              **
  2769. **                                                                     **
  2770. ** REMARKS        : this function does not print a warning if there is **
  2771. **                  an if-statement but no test of the returned value. **
  2772. **                                                                     **
  2773. ** FUNCTIONS      : strncmp(), compile(), find_keyword(), fprintf()    **
  2774. **                                                                     **
  2775. ** GLOBALS        : source_file                                        **
  2776. **                                                                     **
  2777. ** PARAMETERS     : nb_line :number of lines to examine in source_file **
  2778. **                  fout_ptr:points to output file                     **
  2779. **                                                                     **
  2780. ** PRECONDITIONS  : source_file[] must contain an ASCII file with all  **
  2781. **                  '\n' replaced by '\0'.                             **
  2782. **                                                                     **
  2783. ** POSTCONDITIONS : file pointed to by fout_ptr will contain results   **
  2784. **                  of checking process.                               **
  2785. **                                                                     **
  2786. **                                                                     **
  2787. ***C*F*E****************************************************************/
  2788. {
  2789.  CHAR   pbuf1[PMAX];             /* compiled pattern : function called */
  2790.  CHAR   pbuf2[PMAX];             /* compiled pattern : test if         */
  2791.  CHAR   pbuf3[PMAX];             /* compiled pattern : any but space   */ 
  2792.  T_LOCA loca_call;               /* current location of call           */
  2793.  T_LOCA loca_test;               /* current location of test (if)      */
  2794.  T_LOCA loca_sth;                /* current location of sth unexpected */
  2795.  LONG   i;                       /* universal counter                  */
  2796.  LONG   ligne;                   /* start of the search                */
  2797.  CHAR   *start_ptr;              /* start of the search                */
  2798.  
  2799. /*----------- Initialize variables --------------------------------------------*/
  2800.  
  2801.  CHAR *pattern_ptr[] =         /* patterns of functions returning status value */
  2802.  {
  2803.          "malloc(.+;","calloc(.+;","realloc(.+;",
  2804.          "alloca(.+;","_fmalloc(.+;","halloc(.+;",   /* 3 non ANSI std fctions */
  2805.          "strdup(.+;",
  2806.          "fopen(.+;",
  2807.          "the end"
  2808.  };
  2809.  start_ptr = source_file;                                        /* first line */
  2810.  ligne     = 1;
  2811.  
  2812.  compile("^ *if[ (]",pbuf2); 
  2813.  compile("[¡-ÿ!-~]",pbuf3);                      /* ASCII : any char but space */
  2814.  
  2815. /*----------- Look for each library function call -----------------------------*/
  2816.  
  2817.  for ( i=0 ; strncmp(pattern_ptr[i],"the end",7) NEQ 0 ; i++ )
  2818.  {
  2819.     compile(pattern_ptr[i],pbuf1);
  2820.     loca_call = find_keyword(start_ptr, ligne, nb_line, pbuf1);
  2821.  
  2822.     while (loca_call.end_ptr NEQ NULL)
  2823.     {
  2824.        start_ptr = loca_call.end_ptr;
  2825.        ligne     = loca_call.line_no;
  2826.  
  2827.        loca_test = find_keyword(start_ptr, ligne, nb_line, pbuf2);
  2828.        loca_sth  = find_keyword(start_ptr, ligne, nb_line, pbuf3);
  2829.  
  2830.        /*---- warning if there is no if just after the call --------------------*/
  2831.  
  2832.        if ((loca_test.line_no EQ 0) OR (loca_sth.line_no < loca_test.line_no))
  2833.        {
  2834.            fprintf(fout_ptr,"\nWARNING (line %4ld) : the status information returned by this",loca_call.line_no);
  2835.            fprintf(fout_ptr," library function should be checked for validity before use.");
  2836.        }
  2837.  
  2838.        loca_call = find_keyword(start_ptr, ligne, nb_line, pbuf1);
  2839.     }
  2840.     start_ptr = source_file;
  2841.     ligne     = 1;                     /* go back to beginning of source_file[] */
  2842.  }
  2843. }
  2844.  
  2845. void val_and_ad(CHAR source_file[], LONG nb_line, FILE *fout_ptr)
  2846. /**C*F*******************************************************************
  2847. **                                                                     **
  2848. ** SRC-MODULE     : val_and_ad                                         **
  2849. ** VERSION        : 001                                                **
  2850. ** LONG_NAME      : value and address of an object in the same line    **
  2851. **                                                                     **
  2852. **                                                                     **
  2853. ** SPECIFICATION  : C coding conventions                               **
  2854. ** SUBSYSTEM      : -                                                  **
  2855. ** SYSTEM_TYPE    : PCD-3T                                             **
  2856. **                                                                     **
  2857. ** AUTHOR         : Helene Ballay                                      **
  2858. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  2859. **                                                                     **
  2860. ** DESCRIPTION    : val_and_ad() looks for an address (expression      **
  2861. **                  starting with &) reads the corresponding variable  **
  2862. **                  name and look for the variable (not preceeded by &)**
  2863. **                  in the same line. If a match is found, a warning   **
  2864. **                  is printed in output file fout_ptr.                **
  2865. **                                                                     **
  2866. ** REMARKS        : val_and_ad() only checks addresses starting with & **
  2867. **                  followed by lowercase letters, digits and          **
  2868. **                  underscores.                                       **
  2869. **                                                                     **
  2870. ** FUNCTIONS      : compile(), find_keyword(), fprintf(), strncpy(),   **
  2871. **                  strncat()                                          **
  2872. **                                                                     **
  2873. ** GLOBALS        : source_file[]                                      **
  2874. **                                                                     **
  2875. ** PARAMETERS     : nb_line :number of lines to examine in source_file **
  2876. **                  fout_ptr:points to output file                     **
  2877. **                                                                     **
  2878. ** PRECONDITIONS  : source_file[] must contain an ASCII file with all  **
  2879. **                  '\n' replaced by '\0'.                             **
  2880. **                                                                     **
  2881. ** POSTCONDITIONS : file pointed to by fout_ptr will contain results   **
  2882. **                  of checking process.                               **
  2883. **                                                                     **
  2884. **                                                                     **
  2885. ***C*F*E****************************************************************/
  2886. {
  2887.   CHAR   *start_ptr;                    /* place to start the search   */
  2888.   LONG   ligne;                         /* current line                */
  2889.   LONG   end_line;                      /* line to end the search      */
  2890.   CHAR   *s_ptr;                        /* pointer in source_file[]    */
  2891.   CHAR   *end_ptr;                      /* end of address              */
  2892.   T_LOCA l_add;                         /* current location of address */
  2893.   T_LOCA l_val;                         /* current location of value   */
  2894.   CHAR   padd[PMAX];                    /* pattern template : address  */
  2895.   CHAR   pval[PMAX];                    /* pattern template : value    */
  2896.   CHAR   sval[LINLEN];                  /* value : pattern to compile  */
  2897.  
  2898.   /*--------- Initialize variables --------------------------------------------*/
  2899.  
  2900.   start_ptr = source_file;
  2901.   ligne = 1;
  2902.  
  2903.   compile("[^&]&:v", padd);
  2904.  
  2905.   /*--------- Scan source_file[] looking for addresses ------------------------*/
  2906.  
  2907.   l_add = find_keyword(start_ptr, ligne, nb_line, padd);        /* first match */ 
  2908.  
  2909.   while ( l_add.line_no NEQ 0 )                         /* while address found */
  2910.   {
  2911.      ligne     = l_add.line_no;
  2912.      start_ptr = l_add.end_ptr;
  2913.      strncpy(sval,"[ (+*-/=[]",11);                   /* left of value pattern */
  2914.  
  2915.      if (ligne < (nb_line-1))                              /* useful for speed */ 
  2916.          end_line = ligne +1;
  2917.      else
  2918.          end_line = nb_line;
  2919.  
  2920.      /*----- back to start of line (place to start the value search) ----------*/
  2921.  
  2922.      do
  2923.      {
  2924.         start_ptr--;
  2925.      }while( *(start_ptr -1) NEQ '\0');  
  2926.  
  2927.      /*----- find variable name -----------------------------------------------*/
  2928.  
  2929.      s_ptr   = l_add.end_ptr -1;                              /* start of name */
  2930.  
  2931.      end_ptr = s_ptr;               
  2932.      do                                    /* go further til end of name found */
  2933.      {
  2934.          end_ptr++;
  2935.      }while (((*end_ptr < '9') AND (*end_ptr > '0')) OR ((*end_ptr > 'a') AND (*end_ptr < 'z')) OR (*end_ptr EQ '_' ));
  2936.  
  2937.      strncat(sval, s_ptr, (BITFIELD)(end_ptr - s_ptr));/* add var name in sval */
  2938.      strncat(sval,"[ +-*/=);\\]]",14);                 /* add after var name   */
  2939.  
  2940.      /*---- compile and try to find value of var in the same line -------------*/
  2941.  
  2942.      compile(sval,pval);
  2943.      l_val = find_keyword(start_ptr, ligne, end_line, pval);
  2944.      
  2945.      if ( l_val.line_no EQ l_add.line_no )                   /* if match found */
  2946.      {
  2947.         fprintf(fout_ptr,"\nWARNING (line %4ld) : it is better to avoid using both value",l_val.line_no);
  2948.         fprintf(fout_ptr," and address of an object in the same statement.");
  2949.      }
  2950.  
  2951.      start_ptr = l_add.end_ptr;
  2952.      l_add     = find_keyword(start_ptr, ligne, nb_line, padd);   /* next add */
  2953.   }
  2954. }
  2955. void miscellaneous(CHAR source_file[], LONG nb_line, FILE *fout_ptr)
  2956. /**C*F*******************************************************************
  2957. **                                                                     **
  2958. ** SRC-MODULE     : miscellaneous                                      **
  2959. ** VERSION        : 001                                                **
  2960. ** LONG_NAME      : miscellaneous                                      **
  2961. **                                                                     **
  2962. **                                                                     **
  2963. ** SPECIFICATION  : C coding conventions                               **
  2964. ** SUBSYSTEM      : -                                                  **
  2965. ** SYSTEM_TYPE    : PCD-3T                                             **
  2966. **                                                                     **
  2967. ** AUTHOR         : Helene Ballay                                      **
  2968. ** SUBSTITUTE     : Dr. Rainer Storn                                   **
  2969. **                                                                     **
  2970. ** DESCRIPTION    : miscellaneous() checks the presence of definition  **
  2971. **                  of an enumeration type within a structure, of      **
  2972. **                  constants 0 or 0x whose name doesn't start with M_,**
  2973. **                  and of the ? : operators, and prints a warning in  **
  2974. **                  fout_ptr if match is found.                        ** 
  2975. **                                                                     **
  2976. ** REMARKS        : '? is not taken into account as it is assumed      **
  2977. **                  that the statement is parenthesized and it might   **
  2978. **                  not be the ?-C operator (e.g." case '?' : ")       **
  2979. **                                                                     **
  2980. ** FUNCTIONS      : strncmp(), compile(), find_keyword(), fprintf()    **
  2981. **                                                                     **
  2982. ** GLOBALS        : source_file[]                                      **
  2983. **                                                                     **
  2984. ** PARAMETERS     : nb_line :number of lines to examine in source_file **
  2985. **                  fout_ptr:points to output file                     **
  2986. **                                                                     **
  2987. ** PRECONDITIONS  : source_file[] must contain an ASCII file with all  **
  2988. **                  '\n' replaced by '\0'.                             **
  2989. **                                                                     **
  2990. ** POSTCONDITIONS : file pointed to by fout_ptr will contain results   **
  2991. **                  of checking process.                               **
  2992. **                                                                     **
  2993. **                                                                     **
  2994. ***C*F*E****************************************************************/
  2995. {
  2996.   CHAR   pbuf1[PMAX];                /* first compiled pattern         */
  2997.   CHAR   pbuf2[PMAX];                /* second pattern compiled        */
  2998.   CHAR   pbuf3[PMAX];                /* ev. third pattern compiled     */
  2999.   LONG   i;                          /* universal counter              */
  3000.   CHAR   *start_ptr;                 /* start of the search            */
  3001.   LONG   ligne;                      /* start of the search            */
  3002.   LONG   end_line;                   /* end of search                  */
  3003.   T_LOCA loca_1;                     /* current loca of first keyword  */
  3004.   T_LOCA loca_2;                     /* current loca of second keyword */ 
  3005.   T_LOCA loca_3;                     /* current loca of third keyword  */
  3006.  
  3007. /*----------- Initialize variables --------------------------------------------*/
  3008.  
  3009.   CHAR *pattern[]=                                /* first patterns to compile */ 
  3010.   {                    
  3011.           "^ *struct ","#define ",
  3012.           "[^']?",
  3013.           "the_end"
  3014.   };
  3015.   CHAR *patt2[]=                                 /* second patterns to compile */
  3016.   {
  3017.           "}"," 0[1-9x]",
  3018.           "\\:"
  3019.   };
  3020.   CHAR *patt3[]=                                  /* third patterns to compile */
  3021.   {
  3022.           " enum .*{", "M_"
  3023.   };
  3024.   start_ptr = source_file;                                       /* first line */
  3025.   ligne     = 1;
  3026.  
  3027. /*----------- Look for each expression ----------------------------------------*/
  3028.  
  3029.   for ( i = 0 ; strncmp(pattern[i],"the_end",7) NEQ 0 ; i++ )
  3030.   {
  3031.      compile(pattern[i],pbuf1);
  3032.      compile(patt2[i],pbuf2);
  3033.  
  3034.      loca_1 = find_keyword(start_ptr, ligne, nb_line, pbuf1);
  3035.  
  3036.      if ( i<2 )                                    /* struct and enum, or mask */
  3037.      {                                             /* need a third pattern     */
  3038.         compile(patt3[i],pbuf3);
  3039.      }
  3040.  
  3041.      /*----- check each first pattern -----------------------------------------*/
  3042.  
  3043.      while (loca_1.line_no NEQ 0)                    /* while first term found */
  3044.      {
  3045.         start_ptr = loca_1.end_ptr;
  3046.         ligne     = loca_1.line_no;
  3047.  
  3048.         if ( (i>0) AND (ligne < nb_line-1))       /* useful for speed :  except */
  3049.            end_line = ligne + 1;                  /* struct-enum, don't need to */
  3050.         else                                      /* search the other patterns  */
  3051.            end_line = nb_line;                    /* in an other line */
  3052.  
  3053.         loca_2 = find_keyword(start_ptr, ligne, end_line, pbuf2);
  3054.  
  3055.         if (i < 2)                                       /* struct-enum or mask */
  3056.         {
  3057.             loca_3 = find_keyword(start_ptr, ligne, end_line, pbuf3);
  3058.  
  3059.             if ((i EQ 0) AND (loca_3.line_no NEQ 0) AND (loca_3.line_no < loca_2.line_no))
  3060.             {
  3061.                fprintf(fout_ptr,"\nWARNING (line %4ld) : You shouldn't define an enumeration type ",loca_3.line_no);
  3062.                fprintf(fout_ptr,"within a structure.");
  3063.             }
  3064.             if ((i EQ 1) AND (loca_2.line_no EQ loca_1.line_no) AND (loca_3.line_no NEQ loca_2.line_no))
  3065.             {
  3066.                fprintf(fout_ptr,"\nWARNING (line %4ld) : if this constant is serving as bit mask ",loca_1.line_no);
  3067.                fprintf(fout_ptr,"its name should start with M_ ");
  3068.             }
  3069.         }
  3070.         else if ((i EQ 2) AND (loca_1.line_no NEQ 0) AND (loca_1.line_no EQ loca_2.line_no))
  3071.         {
  3072.             fprintf(fout_ptr,"\nWARNING (line %4ld) : it is strongly recommended not to use ",loca_1.line_no);
  3073.             fprintf(fout_ptr,"C's ?-operator but to use if...else constructs if possible.");
  3074.         }
  3075.         loca_1 = find_keyword(start_ptr, ligne, nb_line, pbuf1);
  3076.      }
  3077.      start_ptr = source_file;
  3078.      ligne     = 1;                    /* go back to beginning of source_file[] */
  3079.   }
  3080. }
  3081.